logo头像
Snippet 博客主题

【Leetcode 7】Reverse Integer

难度: 中等(Medium)

题目

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321

代码

1
2
3
4
5
6
7
8
9
10
public int reverse(int x) {
long sum = 0;
while (x != 0) {
sum = sum*10 + x%10;
if (sum>Integer.MAX_VALUE || sum<Integer.MIN_VALUE)
return 0;
x /= 10;
}
return (int)sum;
}

评论系统未开启,无法评论!