Title Description:
Implement atoi to convert a string to an integer.
Hint:carefully consider all possible input cases. If you want a challenge, please don't see below and ask yourself what is the possible input cases.
Notes:it is intended-problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.
Problem Solving Ideas:
This problem is so crazy! Tried many times and failed because there are a number of situations to consider. The main factors to consider are:
- Consider the case where the first or all characters are whitespace characters;
- Consider whether the first character is a +,-, a 0-9 letter, or another character;
- To consider whether there is no worth of overflow, especially the minimum value overflow;
- If the middle has non-numeric characters, to return the previous number, such as "123a34" to return 123;
- If you start with a number of 0, then 0 to ignore, such as "0000234" to return-234
Once you find out about these situations, the code is not difficult to write. In short, the reason why the AC rate is very low is not the logic of how difficult, but it is difficult to consider the whole situation.
Specific code:
Public classSolution { Public Static intmyatoi (String str) {//remove blank characters on both sidesStr=Str.trim (); if(str==NULL|| Str.length () ==0) return0; if(Str.length () ==1){ if(Str.charat (0) >= ' 0 ' && str.charat (0) <= ' 9 ') returninteger.valueof (str); Else return0; } //The first character is a ' + if(Str.charat (0) = = ' + ') {String s= str.substring (1); if(IsValid (s)) {s=Fun (s); if(s==NULL) return0; Mycompatator m=NewMycompatator (); if(M.compare (S, "" +integer.max_value) >=0){ returnInteger.max_value; } Else{ returninteger.valueof (s); } } Else{s=fun1 (s); if(s==NULL) return0; S=Fun (s); if(s==NULL) return0; Mycompatator m=NewMycompatator (); if(M.compare (S, "" +integer.max_value) >=0){ returnInteger.max_value; } Else{ returninteger.valueof (s); } } } //the first character is '- Else if(Str.charat (0) = = '-') {String s= str.substring (1); if(IsValid (s)) {s=Fun (s); if(s==NULL) return0; Mycompatator m=NewMycompatator (); StringBuilder SB=NewStringBuilder ("" +integer.min_value); Sb.deletecharat (0); String SS=sb.tostring (); if(M.compare (S, ss) >=0) returnInteger.min_value; Else{ returninteger.valueof (str); } } Else{s=fun1 (s); if(s==NULL) return0; S=Fun (s); if(s==NULL) return0; Mycompatator m=NewMycompatator (); StringBuilder SB=NewStringBuilder ("" +integer.min_value); Sb.deletecharat (0); String SS=sb.tostring (); if(M.compare (S, ss) >=0) returnInteger.min_value; Else{ returnInteger.valueof ("-" +R); } } } //The first character is a number Else if(Str.charat (0) >= ' 0 ' && str.charat (0) <= ' 9 '){ if(IsValid (str)) {str=Fun (str); if(str==NULL) return0; Mycompatator m=NewMycompatator (); if(M.compare (str, "" +integer.max_value) >=0){ returnInteger.max_value; } Else{ returninteger.valueof (str); } } Else{str=fun1 (str); if(str==NULL) return0; STR=Fun (str); if(str==NULL) return0; Mycompatator m=NewMycompatator (); if(M.compare (str, "" +integer.max_value) >=0){ returnInteger.max_value; } Else{ returninteger.valueof (str); } } } //An error occurs when the first character is a different number Else{ return0; } } //determines whether a string is made up of numbers Public Static BooleanIsValid (String s) {//return S.matches ("[1-9][0-9]*"); returnS.matches ("[0-9]+"); } //Remove all 0 of the beginning of the digital string Public StaticString Fun (string s) {intIndex=0; Booleankey=false; for(Index=0;index<s.length (); index++){ if(S.charat (index)! = ' 0 ') {Key=true; Break; } } if(key) {returns.substring (index); } Else{ return NULL; } } //intercepts the first digit that is not a numeric character Public Staticstring fun1 (string s) {intIndex=0; for(Index=0;s.charat (index) >= ' 0 ' &&s.charat (index) <= ' 9 '; index++){ } if(index==0) return NULL; returnS.substring (0, index); }}classMycompatatorImplementsComparator<string>{@Override Public intCompare (string s1, string s2) {if(S1.length () >s2.length ()) { return1; } Else if(S1.length () <s2.length ()) { return-1; } Else{ for(intI=0;i<s1.length (); i++){ if(S1.charat (i)-S2.charat (i) >0) return1; Else if(S1.charat (i)-S2.charat (i) <0) return-1; Else ; } return0; } }}
"Leetcode" 8. String to Integer (atoi)