Code:
1 classSolution {2 Public:3 intAtoistringstr) {4 intnum =0;5 intSign =1;6 Const intn =str.size ();7 inti =0;8 9 while(Str[i] = =' '&& I <N)Teni++; One A if(Str[i] = ='+') { -i++; - } the Else if(Str[i] = ='-') { -Sign =-1; -i++; - } + - if(Str[i] = ='+'|| Str[i] = ='-') + return 0; A at - for(; i < n; i++) { - if(Str[i] <'0'|| Str[i] >'9') - returnNum *Sign ; - if(Num > Int_max/Ten|| (num = = Int_max/Ten&& (Str[i]-'0') > Int_max%Ten) ){ - returnSign = =-1?Int_min:int_max; in } -num = num *Ten+ Str[i]-'0'; to } + returnNum *Sign ; - } the};
1 classSolution {2 Public:3 intAtoistringstr) {4 intnum =0;5 intSign =1;6 Const intn =str.size ();7 inti =0;8 9 while(Str[i] = =' '&& I <N)Teni++; One A if(Str[i] = ='+') { -i++; - } the Else if(Str[i] = ='-') { -Sign =-1; -i++; - } + - if(Str[i] = ='+'|| Str[i] = ='-') + return 0; A at - for(; i < n; i++) { - if(Str[i] <'0'|| Str[i] >'9') - returnNum *Sign ; - if(Num > Int_max/Ten|| (num = = Int_max/Ten&& (Str[i]-'0') > Int_max%Ten) ){ - returnSign = =-1?Int_min:int_max; in } -num = num *Ten+ Str[i]-'0'; to } + returnNum *Sign ; - } the};
Miscellanies:
1. Pure detail, but feel a lot of details unclear.
[Leetcode] String to Integer (atoi)