Skip to content

Commit 3ec8a90

Browse files
authored
Merge pull request illuz#3 from wanghan0501/master
修复java版本007.Reverse_Integer答案错误
2 parents 96fe372 + 78f91ac commit 3ec8a90

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

solutions/007.Reverse_Integer/AC_simulation_n.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
public class Solution {
1111

1212
public int reverse(int x) {
13-
int ret = 0;
14-
while (Math.abs(x) != 0) {
15-
if (Math.abs(ret) > Integer.MAX_VALUE)
13+
Long res = 0L;
14+
Long tmp = Math.abs((long) x);
15+
while (tmp != 0) {
16+
res = res * 10 + tmp % 10;
17+
tmp = tmp / 10;
18+
if (res > (long)Integer.MAX_VALUE)
1619
return 0;
17-
ret = ret * 10 + x % 10;
18-
x /= 10;
1920
}
20-
return ret;
21+
if (x>0)
22+
return res.intValue();
23+
else
24+
return -res.intValue();
2125
}
2226

2327
// debug

0 commit comments

Comments
 (0)