File tree Expand file tree Collapse file tree 1 file changed +19
-8
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +19
-8
lines changed Original file line number Diff line number Diff line change 2
2
3
3
/**
4
4
* 7. Reverse Integer
5
- *
6
- * Reverse digits of an integer.
7
5
8
- Example1: x = 123, return 321
6
+ Given a 32-bit signed integer, reverse digits of an integer.
9
7
10
- Example2: x = -123, return -321
8
+ Example 1:
9
+ Input: 123
10
+ Output: 321
11
11
12
+ Example 2:
13
+ Input: -123
14
+ Output: -321
15
+
16
+ Example 3:
17
+ Input: 120
18
+ Output: 21
19
+
20
+ Note:
21
+ Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1].
22
+ For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
12
23
*/
13
24
public class _7 {
14
25
15
26
public static class Solution1 {
16
27
public int reverse (int x ) {
17
- long rev = 0 ;
28
+ long result = 0 ;
18
29
while (x != 0 ) {
19
- rev = rev * 10 + x % 10 ;
30
+ result = result * 10 + x % 10 ;
20
31
x /= 10 ;
21
- if (rev > Integer .MAX_VALUE || rev < Integer .MIN_VALUE ) {
32
+ if (result > Integer .MAX_VALUE || result < Integer .MIN_VALUE ) {
22
33
return 0 ;
23
34
}
24
35
}
25
- return (int ) rev ;
36
+ return (int ) result ;
26
37
}
27
38
}
28
39
}
You can’t perform that action at this time.
0 commit comments