-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseInteger.java
More file actions
121 lines (98 loc) · 3.38 KB
/
reverseInteger.java
File metadata and controls
121 lines (98 loc) · 3.38 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
*/
/* Solition 1: */
public class Solution {
public int reverse(int x) {
int result = 0;
int flag = 0;
if(x<0) flag=-1;
x = Math.abs(x);
while(x > 0){
result = result*10 + x%10;
x=x/10;
}
if(result < 0){
return -1;
}
if(flag == -1){
return result*= -1;
}
return result;
}
}
/* The following two solutions comes from Github: Walnutown.*/
/* From JDK1.0, Integer has MIN_VALUE and MAX-VALUE
public static final int MIN_VALUE = 0x80000000;
public static final int MAX_VALUE = 0x7fffffff;
*/
// cast int to long to avoid overflow
public class Solution {
public int reverse(int x) {
int num = Math.abs(x);
long res = 0;
while (num > 0){
res = res*10 + num%10;
num /= 10;
}
if (res>Integer.MAX_VALUE) return x>0 ? Integer.MIN_VALUE : Integer.MAX_VALUE;
return x>0 ? (int)res : (int)-res; // need cast here, otherwise, precision loss warning
}
}
public class Solution {
public int reverse(int x) {
int res = 0;
while (x!=0){
int digit = x%10;
if (digit>Integer.MAX_VALUE%10 && res==Integer.MAX_VALUE/10 || res>Integer.MAX_VALUE/10)
return Integer.MAX_VALUE;
if (digit < Integer.MIN_VALUE%10 && res==Integer.MIN_VALUE/10 || res<Integer.MIN_VALUE/10)
return Integer.MIN_VALUE;
res = res*10 + digit;
x /= 10;
}
return res;
}
}
/*
From this question, think about string reverse. The followings are some solution about string reverse.
Thanks to http://www.importnew.com/501.html
*/
/* Solution 1: StringBuffer */
public class JdkReverser implements Reverser {
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return new StringBuffer(str).reverse().toString();
}
}
/* Solution 2: Recursion */
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
/* Solution 3: */
public String reverse(String str) {
if ((null == str) || (str.length() <= 1 )) {
return str;
}
StringBuffer result = new StringBuffer(str);
for (int i = 0; i < (str.length() / 2); i++) {
int swapIndex = str.length() - 1 - i;
char swap = result.charAt(swapIndex);
result.setCharAt(swapIndex, result.charAt(i));
result.setCharAt(i, swap);
}
return result.toString();
}
/* There is also a question about "Reverse Words in a String" on Leetcode.*/