-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL7.java
More file actions
38 lines (32 loc) · 878 Bytes
/
L7.java
File metadata and controls
38 lines (32 loc) · 878 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
32
33
34
35
36
37
38
package com.liang.leetcode;
/**
* @ClassName L7
* @description reverse-integer
* @Author LiaNg
* @Date 2018/12/6
*/
public class L7 {
public static void main(String[] args) {
int x = 120;
L7 l = new L7();
System.out.println(l.reverse(x));
}
/**
* 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
*/
public 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 / 10 && pop > 7)) {
return 0;
}
if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) {
return 0;
}
rev = rev * 10 + pop;
}
return rev;
}
}