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.
2 parents 96fe372 + 78f91ac commit 3ec8a90Copy full SHA for 3ec8a90
solutions/007.Reverse_Integer/AC_simulation_n.java
@@ -10,14 +10,18 @@
10
public class Solution {
11
12
public int reverse(int x) {
13
- int ret = 0;
14
- while (Math.abs(x) != 0) {
15
- if (Math.abs(ret) > Integer.MAX_VALUE)
+ Long res = 0L;
+ Long tmp = Math.abs((long) x);
+ while (tmp != 0) {
16
+ res = res * 10 + tmp % 10;
17
+ tmp = tmp / 10;
18
+ if (res > (long)Integer.MAX_VALUE)
19
return 0;
- ret = ret * 10 + x % 10;
- x /= 10;
20
}
- return ret;
21
+ if (x>0)
22
+ return res.intValue();
23
+ else
24
+ return -res.intValue();
25
26
27
// debug
0 commit comments