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.
Requirements: String into a number, need to identify the sign
""
=>0
-1
=>-1
+-1
=>0
1234567890123456789012345678901234567890
=>0
"2147483648"
=>2147483647
Public intmyatoi (String str) {str=Str.trim (); if(NULL==str | | "". Equals (str))return0; CharFlag = ' + '; inti = 0; if(Str.charat (0) = = ' + ') {flag= ' + '; I++; }Else if(Str.charat (0) = = '-') {flag= '-'; I++; } Doubleresult = 0.0; if(Str.length () >12) return0; while(Str.length () >i && Str.charat (i) >= ' 0 ' && str.charat (i) <= ' 9 ') {result= result * + (Str.charat (i)-' 0 '); I++; } if(flag== '-') Result= -result; if(result>integer.max_value) Result=Integer.max_value; if(result<integer.min_value) Result=Integer.min_value; return(int) result; }
[Leetcode]-algorithms-string to Integer (atoi)