Skip to content

Commit

Permalink
ReverseInteger
Browse files Browse the repository at this point in the history
Refine ReverseInteger
  • Loading branch information
mengli committed Oct 26, 2017
1 parent fd8cfb2 commit f815fcf
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions ReverseInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@

public class ReverseInteger {
public int reverse(int x) {
long num = Math.abs(x);
long l = x;
long num = Math.abs(l);
long ret = 0;
while (num != 0) {
int d = num - num / 10 * 10;
long d = num - num / 10 * 10;
ret = ret * 10 + d;
num /= 10;
}
if (x < 0)
return -ret < -2147483648 ? 0 : -ret;
return (int) (-ret < -2147483648 ? 0 : -ret);
else
return ret > 2147483647 ? 0 : ret;
return (int) (ret > 2147483647 ? 0 : ret);
}
}

0 comments on commit f815fcf

Please sign in to comment.