Skip to content

Commit abf239c

Browse files
author
luojing
committed
add 0007 solution
1 parent 93e4b24 commit abf239c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.blankj.easy._007;
2+
3+
/**
4+
* @author luojing
5+
* @description LJSolution
6+
* @date 2022/07/15
7+
*/
8+
9+
public class LJSolution {
10+
public int reverse(int x) {
11+
long res = 0;
12+
for (; x != 0 ; x /= 10) {
13+
/**
14+
* 0. 除10一循环,直到除不了10后
15+
* 1. 先获取x的余数,也就是最后一位,依次类推获取新的最后一位
16+
* 2. 获取最后一位数后再按次数乘以10,就将整形数反转了
17+
* 3. 溢出返回0,返回时判断下是否会溢出
18+
* 4. 注意0*10还是0,不存在反转后001这种数
19+
*/
20+
res = res * 10 + x%10;
21+
}
22+
return res > Integer.MAX_VALUE || res<Integer.MIN_VALUE ? 0 : (int) res;
23+
}
24+
25+
public static void main(String[] args) {
26+
LJSolution ljSolution = new LJSolution();
27+
System.out.println(ljSolution.reverse(123));
28+
System.out.println(ljSolution.reverse(-123)); //负数
29+
System.out.println(ljSolution.reverse(100)); //末尾有0
30+
System.out.println(ljSolution.reverse(1000000003)); //溢出
31+
}
32+
}

0 commit comments

Comments
 (0)