There are no algorithms to say, but it is to be considered well-rounded.
1. It is possible that ans is more than a long long so overflow may not be positive or negative, so first judge more than 10 digits directly output the edge value.
2. The previous part does not conform to the provisions of return 0, there are a few cases are not: +-2,-992 (minus and the first number before a space, it will not be). But +5455 of these are right.
3.5545a55 this, a and its back are not counted.
classSolution { Public: intMyatoi (stringstr) { intlen=str.length (); stringSt; intfu=2; for(intI=0; i<len;i++){ if(st.length () = =0&& (!) ( str[i]>='0'&&str[i]<='9'))){ if(str[i]==' '&&fu==2){ Continue; } Else if(str[i]=='-'&&fu==2) {Fu=1; } Else if(str[i]=='+'&&fu==2) {Fu=0; } Else return 0; } Else if(St.length () >0&& (!) ( str[i]>='0'&&str[i]<='9'))){ Break; } Else if(str[i]>='0'&&str[i]<='9') {St+=str.substr (I,1); } } intLl=st.length (); if(ll==0)return 0; Long Long intans=0, shi=1; if(ll>Ten){ if(fu==1)return-2147483648; Else return 2147483647; } for(inti=ll-1; i>=0; i--) {ans+=shi* (st[i]-'0'); Shi*=Ten; } if(fu==1) ans=-ans; if(ans>2147483647)return 2147483647; Else if(ans<-2147483648)return-2147483648; Else return(int) ans; }};
Leetcode 8 String to Integer (atoi)