String to Integer (atoi)Accepted:52232 Total submissions:401038 My Submissions Question
Solution
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 for the problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click on the Reload button to reset your co De definition.
Spoilers alert ... click to show requirements for atoi.
Requirements for Atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, the starting from this character, takes a optional initial plus or minus sign followed by as many numerical digits as P Ossible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which is ignored and has no Effe CT on the behavior of this function.
If the first sequence of non-whitespace characters in STR isn't a valid integral number, or if no such sequence exists be Cause either str is empty or it contains only whitespace characters, no conversion is performed.
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 Returne D.
C + + Solutions:
Class Solution {Public://consider a case: " +-++--3" int myatoi (string str) { long result = 0; int indicator = 1; for (int i = 0; I<str.size ();) { i = str.find_first_not_of ("); if (str[i] = = '-' | | str[i] = = ' + ') indicator = (str[i++] = = '-')? -1:1; while (' 0 ' <= str[i] && str[i] <= ' 9 ') { result = result*10 + (str[i++]-' 0 '); if (result*indicator >= Int_max) return int_max; if (result*indicator <= int_min) return int_min; } return result*indicator;}} ;
Python solution:
Class Solution: # @return An integer def atoi (self, str): string = str buf = "# our list dg_list = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '] dg_signal = ['-', ' + '] signal = ' #there is no +/-0-9 at first no_signal = True No_dig = True #strip whitespace for I in String.strip () : #if i in Dg_signal judge: #1: It's the first-/+ and signal =-/+, then set no_signal = False. Start to Next i #2: it It's the second-/+ so it's wrong,we return 0 if I in dg_signal: If No_signal is False:return 0 if no_signal:signal + = i No_signal=false continue #if I was 0-9, we save it to buf if I in dg_list : Buf+=i #but If there is a-/+, and the next char isn't dig, eg:+a here we break. Elif No_signal is FaLse:break else: #if It isn't in Dg_list and Dg_signal,and It's also has Someth ing eg:+322a99 when i = A and buf = +322. We jsut break A and continue #add buf if Len (buf) >0:break #if len (buf) <0. Eg:-A. We return as the Sys tips if Len (buf) <=0:return 0 #add +/-If Len (BUF) >0:buf=signal+buf If Len (buf) <=0:return 0 f_result = Int (buf) if F_resu Lt >2147483647:return 2147483647 if f_result<-2147483648:return-2147483648 re Turn F_result
Python Solution 2:
Class solution:# @return an integerdef atoi (self, str): str = str.strip () str = Re.findall (' (^[\+\-0]*\d+) \d* ', s TR) try: result = Int (". Join (str)) max_int = 2147483647 min_int = -2147483648 if result > Max_ INT > 0: return max_int elif result < Min_int < 0: return min_int else: return RESULT
except: return 0
Python Solution 3:
Class solution:# @return an integerdef atoi (self, str): str = Str.strip () If Len (str) = = 0:return 0 R, I, L, s = 0, 0, 0, ' if str[0] in ' +-': s = str[0] i = 1 for i in xrange (i, Len (str)): if ' 0 ' <= str[i] <= ' 9 ': r = r*10 + ord (Str[i])-ord (' 0 ') l + = 1 else: Break if r = = 0 and (s or L = = 0): r Eturn 0 Elif r > 0 and s = = '-': R *=-1 if R > 2147483647: r = 2147483647 if r <-21474 83648: r = -2147483648 return r
Leetcode 8 String to Integer (atoi)