Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit823401c

Browse files
accepted for StringToInteger
1 parenta6f9f5a commit823401c

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

‎EASY/src/easy/StringToInteger.java

Lines changed: 50 additions & 11 deletions
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
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
2427
publicintmyAtoi(Stringstr) {
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
StringBuildersb =newStringBuilder();
4447
booleannegative;
48+
intminuSignCount =0,plusSignCount =0;
4549
inti =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+
}elseif(chars[i] =='+'){
55+
plusSignCount++;
56+
i++;
57+
}else {
58+
break;
59+
}
4960
}
50-
elsenegative =false;
61+
if((plusSignCount >0 &&minuSignCount >0) ||minuSignCount >1 ||plusSignCount >1)return0;
62+
negative =minuSignCount%2 !=0;
63+
if(i >=chars.length)return0;
5164

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

73+
if(sb ==null ||sb.length() ==0)return0;
74+
6075
intresult =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(Exceptione){
73-
System.out.println(e);
74-
}
84+
//use Long to parse to handle integer overflow case
85+
longtemp =0;
86+
if(sb.length() >=Long.toString(Long.MAX_VALUE).length() &&negative){
87+
returnInteger.MIN_VALUE;
88+
}elseif(sb.length() >=Long.toString(Long.MAX_VALUE).length() && !negative){
89+
returnInteger.MAX_VALUE;
90+
}else {
91+
try{
92+
temp =Long.parseLong(sb.toString());
93+
}catch(Exceptione){
94+
if(sb.length() >=Integer.MAX_VALUE)result =Integer.MAX_VALUE;
95+
}
96+
if(temp > (long)Integer.MAX_VALUE+1) {
97+
if(!negative)returnInteger.MAX_VALUE;
98+
elsereturnInteger.MIN_VALUE;
99+
}
100+
elseif(temp == (long)Integer.MAX_VALUE+1 &&negative)returnInteger.MIN_VALUE;
101+
elseif(temp == (long)Integer.MAX_VALUE+1)returnInteger.MAX_VALUE;
102+
elseif(temp <Integer.MIN_VALUE)result =Integer.MIN_VALUE;
103+
elseresult = (int)temp;
104+
}
75105
}
76106

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

80111
publicstaticvoidmain(String...strings){
81112
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
120+
// String str = "-2147483649";
121+
Stringstr ="9223372036854775809";
83122
System.out.println(test.myAtoi(str));
84123

85124

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp