We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 191c61c commit ae38b95Copy full SHA for ae38b95
java/7-Reverse-Integer.java
@@ -1,25 +1,19 @@
1
class Solution {
2
-
3
public int reverse(int x) {
4
- int min = Integer.MIN_VALUE;
5
- int max = Integer.MAX_VALUE;
+ boolean isNegative = x < 0;
6
7
- int res = 0;
8
- while (x != 0) {
9
- int digit = x % 10;
10
- x = x / 10;
+ x = Math.abs(x);
11
12
- if (
13
- (res > max / 10) || (res == max / 10 && digit >= max % 10)
14
- ) return 0;
+ int num = 0;
15
16
17
- (res < min / 10) || (res == min / 10 && digit <= min % 10)
18
+ while(x > 0) {
+ if (Integer.MAX_VALUE/10 < num)
+ return 0;
19
20
- res = (res * 10) + digit;
+ num = 10*num + x%10;
+ x /= 10;
21
}
22
23
- return res;
+ return isNegative? -num : num;
24
25
-}
+}
0 commit comments