1. 程式人生 > >Leetcode:8- String to Integer (atoi)

Leetcode:8- String to Integer (atoi)

integer font please because sta digits 翻譯 isspace time

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

關於atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because 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 returned.

直接把谷歌翻譯粘過來:

該函數首先丟棄盡可能多的空白字符,直到找到第一個非空白字符。 然後,從這個字符開始,采用一個可選的初始加號或減號,後面跟隨盡可能多的數字,並將其解釋為一個數字值。

該字符串可以包含附加字符後面的那些形成整數的數字,這些字符被忽略,對這個函數的行為沒有影響。

如果str中的第一個非空白字符序列不是有效整數,或者由於str為空或只包含空格字符而不存在這樣的序列,則不執行轉換。

如果不能執行有效的轉換,則返回零值。 如果正確值超出了可表示值的範圍,則返回INT_MAX(2147483647)或INT_MIN(-2147483648)。

思路:

(1)跳過前面的空格、制表符等,可以用isspace()判斷

(2)記錄該數的正負

(3)遇到非數字字符後跳出循環並輸出前面的數字

(4)邊界:判斷當前數字再升一位是否溢出,溢出則返回邊界值。若不溢出先升位,再判斷是否加當前位的值,加上不溢出則加上,溢出則輸出邊界。原因在於如果超出範圍就返回最接近的 int 數。

 1 class Solution(object):
 2     def myAtoi(self, str):
 3         INT_MAX = 2147483647
 4         INT_MIN = -2147483648
 5         sum = 0
 6         sign = 1
 7         i = 0
 8         if str == ‘‘:
 9             return 0
10         while i < len(str) and str[i].isspace():  #掃描的字符是空格/回車/制表符等
11             i += 1
12         if i < len(str) and str[i] == -:
13             sign = -1
14         if i < len(str) and (str[i] == - or str[i] == +):
15             i += 1
16         while i < len(str) and str[i].isdigit():  #掃描的字符是0~9的數字
17             digit = int(str[i])
18             if INT_MAX / 10 >= sum:   #INT_MAX/10 >= sum和INT_MAX - digit >= sum這兩個判斷條件確保了不會溢出。
19                 sum *= 10   #此處是判斷sum*10(當前的str[i]未加)是否溢出,不溢出則進行該操作
20             else:
21                 if sign == 1:
22                     return INT_MAX  #否則輸出邊界值
23                 else:
24                     return INT_MIN
25             if INT_MAX - digit >= sum:  #此處是判斷sum+str[i]是否溢出,不溢出則加上,溢出則輸出邊界
26                 sum += digit
27             else:
28                 if sign == 1:
29                     return INT_MAX
30                 else:
31                     return INT_MIN
32             i += 1
33         return sign * sum
34 
35 if __name__==__main__:
36     s = 1
37     solution = Solution()
38     print(solution.myAtoi(s))

Leetcode:8- String to Integer (atoi)