-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseInteger.java
More file actions
61 lines (54 loc) · 1.18 KB
/
ReverseInteger.java
File metadata and controls
61 lines (54 loc) · 1.18 KB
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
60
61
package com.leetcode;
/**
* Created by jamylu on 2017/12/31.
* leetcode007.
*/
public class ReverseInteger {
public static void main(String args[]) {
int n = -67670;
System.out.println(reverse2(n));
}
public static int reverse2(int x) {
int result = 0;
if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) {
return result;
}
while (x != 0) {
int tail = x % 10;
result = result * 10 + tail;
x /= 10;
}
return result;
}
//不应该转化成字符串
/*
public static int reverse(int x) {
if (x == 0) {
return 0;
}
int flag = 0;
String str = "";
int tmp;
if (x < 0) {
flag = 1;
x = -x;
}
if (x % 10 == 0) {
} else {
tmp = x % 10;
str += tmp;
}
x /= 10;
while (x > 0) {
tmp = x % 10;
str += tmp;
x /= 10;
}
if (flag == 1) {
return -Integer.parseInt(str);
} else {
return Integer.parseInt(str);
}
}
*/
}