Repository navigation
Expand file tree
/
Copy path7_Reverse_Integer.java
More file actions
55 lines (47 loc) · 1.53 KB
/
Copy path7_Reverse_Integer.java
File metadata and controls
55 lines (47 loc) · 1.53 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
*Given a 32-bit signed integer, reverse digits of an integer.
*
*Example 1:
*Input: 123
*Output: 321
*
*Example 2:
*Input: -123
*Output: -321
*
*Example 3:
*Input: 120
*Output: 21
*
*Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose
*of this problem, assume that your function returns 0 when the reversed integer overflows.
*/
class Solution {
public int reverse(int x) {
int ret = 0;
int digit = 0;
boolean neg_flag = false;
if (x < 0) {
neg_flag = true;
x = -1 * x; //covert to abs(x), and record the symbol of negative or positive.
}
while (x != 0) {
digit = x % 10; //get the last digit of x
if (ret != 0) { //must follow this pattern to check
if ((Integer.MAX_VALUE - digit) / ret < 10 )
return 0;
if (neg_flag == true) {
if (-10 < (Integer.MIN_VALUE + digit) / ret)
// - (ret * 10 + digit) < Integer.MIN_VALUE
//if we convert the number to abs, we need to compare it in negative form with Integer.MIN_VALUE
return 0;
}
}
ret = ret * 10 + digit;
x = x / 10; //chop off the last digit of x
}
if (neg_flag == true && ret > 0)
ret = -1 * ret;
return ret;
}
}