-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseInteger.java
More file actions
28 lines (25 loc) · 829 Bytes
/
ReverseInteger.java
File metadata and controls
28 lines (25 loc) · 829 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
package com.cier.solution.string;
// https://leetcode-cn.com/problems/reverse-integer/description/
public class ReverseInteger {
public static int reverse(int x) {
int rev = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE && pop > 7)) {
return 0;
}
if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE && pop < -8)) {
return 0;
}
rev = rev * 10 + pop;
}
return rev;
}
public static void main(String[] args) {
System.out.println(reverse(1534236469));
System.out.println("end");
System.out.println(Math.pow(-2, 31));
System.out.println(Math.pow(2, 31) - 1);
}
}