Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
59 lines (54 loc) · 1.24 KB

File metadata and controls

59 lines (54 loc) · 1.24 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
56
57
58
59
package leetcode.all.solution1_100;
/**
* 反转整数.
* 给定一个 32 位有符号整数,将整数中的数字进行反转。
* <p>
* 示例 1:
* 输入: 123
* 输出: 321
* <p>
* 示例 2:
* 输入: -123
* 输出: -321
* <p>
* 示例 3:
* 输入: 120
* 输出: 21
* <p>
* 注意:
* 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231 − 1]。根据这个假设,如果反转后的整数溢出,则返回 0。
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class Solution7 {
/**
* 用long保存结果.从低位开始计算,注意负数的处理
* @param x
* @return
*/
public int reverse(int x) {
int head=1;
if(x<0) {
x=-x;
head=-1;
}
int t=x%10;
long r=0;
x=x/10;
while(x!=0){
r=(r+t)*10;
t=x%10;
x=x/10;
}
r=(r+t);
if((head*r>(Math.pow(2,31)-1))||(head*r<-1*(Math.pow(2,31)-1))) return 0;
return (int)(head*r);
}
public static void main(String[] args){
int x=-123;
int r=new Solution7().reverse(x);
System.out.println(r);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.