Skip to content

Commit 823401c

Browse files
accepted for StringToInteger
1 parent a6f9f5a commit 823401c

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

EASY/src/easy/StringToInteger.java

+50-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
2222
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.*/
2323
public class StringToInteger {
24+
//TODO: look at others' solutions
25+
26+
//Eventually, made it AC'ed, lots of corner cases, but now, really felt much easier and the though process is super clear than the first time I tried to solve it which was 3~4 years ago from now. 8/9/2016
2427
public int myAtoi(String str) {
2528
//case 1: str is greater than Integer.MAX_VALUE, return Integer.MAX_VALUE as the question states it
2629

@@ -42,12 +45,22 @@ public int myAtoi(String str) {
4245
char[] chars = str.toCharArray();
4346
StringBuilder sb = new StringBuilder();
4447
boolean negative;
48+
int minuSignCount = 0, plusSignCount = 0;
4549
int i = 0;
46-
if(chars[0] == '-') {
47-
negative = true;
48-
i++;//let i start from 1 in this case
50+
while(i < chars.length){
51+
if(chars[i] == '-'){
52+
minuSignCount++;
53+
i++;
54+
} else if(chars[i] == '+'){
55+
plusSignCount++;
56+
i++;
57+
} else {
58+
break;
59+
}
4960
}
50-
else negative = false;
61+
if((plusSignCount > 0 && minuSignCount > 0) || minuSignCount > 1 || plusSignCount > 1) return 0;
62+
negative = minuSignCount%2 != 0;
63+
if(i >= chars.length) return 0;
5164

5265
//it might be a floating number, so consider '.'
5366
int period = 0;
@@ -57,6 +70,8 @@ public int myAtoi(String str) {
5770
sb.append(chars[i++]);
5871
}
5972

73+
if(sb == null || sb.length() == 0) return 0;
74+
6075
int result = 0;
6176
if(period > 0){
6277
//use Double to parse
@@ -66,20 +81,44 @@ public int myAtoi(String str) {
6681
System.out.println(e);
6782
}
6883
} else {
69-
//use Integer to parse
70-
try{
71-
result = Integer.parseInt(sb.toString());
72-
} catch(Exception e){
73-
System.out.println(e);
74-
}
84+
//use Long to parse to handle integer overflow case
85+
long temp = 0;
86+
if(sb.length() >= Long.toString(Long.MAX_VALUE).length() && negative){
87+
return Integer.MIN_VALUE;
88+
} else if(sb.length() >= Long.toString(Long.MAX_VALUE).length() && !negative){
89+
return Integer.MAX_VALUE;
90+
} else {
91+
try{
92+
temp = Long.parseLong(sb.toString());
93+
} catch(Exception e){
94+
if(sb.length() >= Integer.MAX_VALUE) result = Integer.MAX_VALUE;
95+
}
96+
if(temp > (long) Integer.MAX_VALUE+1) {
97+
if(!negative)return Integer.MAX_VALUE;
98+
else return Integer.MIN_VALUE;
99+
}
100+
else if(temp == (long) Integer.MAX_VALUE+1 && negative) return Integer.MIN_VALUE;
101+
else if(temp == (long) Integer.MAX_VALUE+1) return Integer.MAX_VALUE;
102+
else if(temp < Integer.MIN_VALUE) result = Integer.MIN_VALUE;
103+
else result = (int) temp;
104+
}
75105
}
76106

107+
if(negative) result = -result;
77108
return result;
78109
}
79110

80111
public static void main(String...strings){
81112
StringToInteger test = new StringToInteger();
82-
String str = "1";
113+
// String str = "2147483648";
114+
// String str = "+-2";//a really interesting test case, you never know how stupid one's input could be like, this is to challenge your program to be more robust. It's expecting to return 0 for this case which means it's not a valid number
115+
// String str = "+";
116+
// String str = "abc";
117+
// String str = "1";
118+
// String str = "-2147483648";
119+
// String str = "++1";//I'm really amazed by OJ's test case variety, it's expecting 0 in this case
120+
// String str = "-2147483649";
121+
String str = "9223372036854775809";
83122
System.out.println(test.myAtoi(str));
84123

85124

0 commit comments

Comments
 (0)