-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path7.reverse-integer.java
More file actions
32 lines (26 loc) · 705 Bytes
/
7.reverse-integer.java
File metadata and controls
32 lines (26 loc) · 705 Bytes
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
/*
* @lc app=leetcode id=7 lang=java
*
* [7] Reverse Integer
*/
// @lc code=start
class Solution {
public int reverse(int x) {
//Integer.MAX_VALUE = 2147483647
//Integer.MIN_VALUE = -2147483648
int result = 0;
while (x != 0) {
int pop = x % 10;
x = x / 10;
if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && pop > 7)) {
return 0;
}
if (result < Integer.MIN_VALUE / 10 || (result == Integer.MIN_VALUE / 10 && pop < -8)) {
return 0;
}
result = result * 10 + pop;
}
return result;
}
}
// @lc code=end