Skip to content

Commit fd8cfb2

Browse files
committed
ReverseInteger
Refine ReverseInteger solution so that it can pass all test cases.
1 parent 04d0767 commit fd8cfb2

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

ReverseInteger.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121

2222
public class ReverseInteger {
2323
public int reverse(int x) {
24-
int num = Math.abs(x);
25-
int ret = 0;
24+
long num = Math.abs(x);
25+
long ret = 0;
2626
while (num != 0) {
2727
int d = num - num / 10 * 10;
2828
ret = ret * 10 + d;
2929
num /= 10;
3030
}
3131
if (x < 0)
32-
return -ret;
32+
return -ret < -2147483648 ? 0 : -ret;
3333
else
34-
return ret;
34+
return ret > 2147483647 ? 0 : ret;
3535
}
3636
}

ZigZagConversion.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public String convert(String s, int numRows) {
2222
if (numRows <= 1) {
2323
return s;
2424
}
25-
// Row index of each char in ZigZag form
25+
// Row index of each char in ZigZag form
2626
int[] rowIndex = new int[s.length()];
2727
int row = -1;
28-
boolean down = true; // Simlate the movement of ZigZag, up or down
28+
// Simlate the movement of ZigZag, up or down
29+
boolean down = true;
2930
for (int i = 0; i < rowIndex.length; i++) {
3031
if (down) {
3132
row += 1;

0 commit comments

Comments
 (0)