Skip to content

Commit 98a399e

Browse files
refactor 7
1 parent 86dc08a commit 98a399e

File tree

1 file changed

+19
-8
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+19
-8
lines changed

src/main/java/com/fishercoder/solutions/_7.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@
22

33
/**
44
* 7. Reverse Integer
5-
*
6-
* Reverse digits of an integer.
75
8-
Example1: x = 123, return 321
6+
Given a 32-bit signed integer, reverse digits of an integer.
97
10-
Example2: x = -123, return -321
8+
Example 1:
9+
Input: 123
10+
Output: 321
1111
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.
1223
*/
1324
public class _7 {
1425

1526
public static class Solution1 {
1627
public int reverse(int x) {
17-
long rev = 0;
28+
long result = 0;
1829
while (x != 0) {
19-
rev = rev * 10 + x % 10;
30+
result = result * 10 + x % 10;
2031
x /= 10;
21-
if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) {
32+
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
2233
return 0;
2334
}
2435
}
25-
return (int) rev;
36+
return (int) result;
2637
}
2738
}
2839
}

0 commit comments

Comments
 (0)