8000 完成String转换int的函数。 · fatherican/javaalgorithm@073f4e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 073f4e5

Browse files
author
shengshijun
committed
完成String转换int的函数。
1 parent 2419492 commit 073f4e5

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

src/main/java/ssj/algorithm/math/MathUtil.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,35 @@ public static int uglyNumber(int index) {
219219
return ugly_numbers[index - 1];
220220
}
221221

222+
public static int strToInt(String str) {
223+
Preconditions.checkNotNull(str);
224+
Preconditions.checkArgument(str.length() > 0);
225+
Preconditions.checkArgument(!(str.length() == 1 && (str.charAt(0) == '+' || str.charAt(0) == '-')));
226+
int result = 0;
227+
boolean is_negative = false;
228+
int num_start = 0;
229+
if (str.charAt(0) == '-') {
230+
is_negative = true;
231+
num_start = 1;
232+
} else if (str.charAt(0) == '+') {
233+
num_start = 1;
234+
}
235+
236+
for (int i = num_start; i < str.length(); i++) {
237+
int this_digit = str.charAt(i) - '0';
238+
if (this_digit < 0 || this_digit > 9) {
239+
throw new ArithmeticException("wrong format");
240+
}
241+
result = Math.multiplyExact(result, 10);
242+
if (is_negative) {
243+
result = Math.subtractExact(result, this_digit);
244+
} else {
245+
result = Math.addExact(result, this_digit);
246+
}
247+
}
248+
return result;
249+
}
250+
222251
public int closestBig(int origion) {
223252
Preconditions.checkArgument(origion > 0);
224253
int first_one_index = BitUtil.firstBitOne(origion);

src/main/java/ssj/algorithm/string/StringUntil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static boolean uniqueChar(String s) {
6767

6868
/**
6969
* 判断是不是变位词。
70+
*
7071
* @param s1
7172
* @param s2
7273
* @return
@@ -174,6 +175,7 @@ public int search(String str) {
174175
* 假设字符串平均长度m,字符串个数n。
175176
* 第一步复杂度是O(nmlgm),第二步复杂度是O(n)
176177
* 所以算法总体的复杂度就是O(nmlgm)
178+
*
177179
* @param words
178180
* @return
179181
*/

src/test/java/ssj/algorithm/Main.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package ssj.algorithm;
22

3-
import ssj.algorithm.math.MathUtil;
4-
53
/**
64
* Created by shenshijun on 15/2/1.
75
*/
86
public class Main {
97

108
public static void main(String[] args) {
11-
System.out.println(MathUtil.uglyNumber(1500));
9+
System.out.println(Integer.MAX_VALUE + 2L);
10+
// int i = 2147483648;
1211
}
1312
}

src/test/java/ssj/algorithm/math/MathUtilTest.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import org.junit.Test;
44
import ssj.algorithm.lang.Tuple2;
55

6-
import static org.junit.Assert.assertArrayEquals;
7-
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.*;
87

98
/**
109
* Created by shenshijun on 15/2/26.
@@ -29,4 +28,53 @@ public void testCountNumberOne() {
2928
public void testCombineMinNumber() {
3029
assertArrayEquals(new Integer[]{321, 32, 3}, MathUtil.combineMinNumber(new Integer[]{3, 32, 321}));
3130
}
31+
32+
@Test
33+
public void testStrToInt() {
34+
assertEquals(10000, MathUtil.strToInt("10000"));
35+
assertEquals(-10000, MathUtil.strToInt("-10000"));
36+
assertEquals(0, MathUtil.strToInt("-0"));
37+
assertEquals(0, MathUtil.strToInt("+0"));
38+
assertEquals(Integer.MAX_VALUE, MathUtil.strToInt(String.valueOf(Integer.MAX_VALUE)));
39+
assertEquals(Integer.MIN_VALUE, MathUtil.strToInt(String.valueOf(Integer.MIN_VALUE)));
40+
try {
41+
MathUtil.strToInt("+");
42+
fail("never go here");
43+
} catch (Exception e) {
44+
}
45+
46+
try {
47+
MathUtil.strToInt("-");
48+
fail("never go here");
49+
} catch (Exception e) {
50+
51+
}
52+
53+
try {
54+
MathUtil.strToInt("");
55+
fail("never go here");
56+
} catch (Exception e) {
57+
}
58+
59+
try {
60+
MathUtil.strToInt("-10292a292983");
61+
fail("never go here");
62+
} catch (Exception e) {
63+
64+
}
65+
66+
try {
67+
MathUtil.strToInt(String.valueOf(Integer.MAX_VALUE + 10L));
68+
fail("never go here");
69+
} catch (Exception e) {
70+
71+
}
72+
73+
try {
74+
MathUtil.strToInt(String.valueOf(Integer.MIN_VALUE - 10L));
75+
fail("never go here");
76+
} catch (Exception e) {
77+
78+
}
79+
}
3280
}

0 commit comments

Comments
 (0)
0