-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReverseInteger.java
26 lines (25 loc) · 927 Bytes
/
ReverseInteger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Given a 32-bit signed integer, reverse digits of an integer.
* Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1].
* For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
* @author LBW
*/
public class ReverseInteger {
public int reverse(int x) {
int result = 0;
while (x != 0) {
int t = x % 10;
x = x/10;
//check whether result is going to overflow.
if (t > 0 && result > (Integer.MAX_VALUE - t) / 10 )
return 0;
if (t < 0 && result < (Integer.MIN_VALUE - t) / 10)
return 0;
result = result * 10 + t;
}
return result;
}
public static void main(String[] args) {
ReverseInteger reverseInteger = new ReverseInteger();
}
}