You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: EASY/src/easy/StringToInteger.java
+50-11
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,9 @@
21
21
22
22
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.*/
23
23
publicclassStringToInteger {
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
24
27
publicintmyAtoi(Stringstr) {
25
28
//case 1: str is greater than Integer.MAX_VALUE, return Integer.MAX_VALUE as the question states it
26
29
@@ -42,12 +45,22 @@ public int myAtoi(String str) {
elseif(temp < Integer.MIN_VALUE) result = Integer.MIN_VALUE;
103
+
elseresult = (int) temp;
104
+
}
75
105
}
76
106
107
+
if(negative) result = -result;
77
108
returnresult;
78
109
}
79
110
80
111
publicstaticvoidmain(String...strings){
81
112
StringToIntegertest = newStringToInteger();
82
-
Stringstr = "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
0 commit comments