atoi 函数…比较经典的面试坑题。
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.
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 the reload button to reset your code definition.
spoilers alert… click to show requirements for atoi.
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.
反正我没看上面那些,自己慢慢测试呗。
最初代码如下:简直各种…呵呵呵。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ if len(str) == 0 : return 0 idx = 0 for i in range(len(str)) : if str[i] == ' ' : idx += 1 else : break str = str[idx:] #print(str) rt = 0 if str[0] == '-' : for i in range(1, len(str)): if str[i].isdigit() : rt = rt * 10 - int(str[i]) else : break elif str[0] == '+' : for i in range(1, len(str)): if str[i].isdigit() : rt = rt * 10 + int(str[i]) else : break else : for i in range(len(str)): if str[i].isdigit() : rt = rt * 10 + int(str[i]) else : break if rt < -(math.pow(2, 31)) : return int(-(math.pow(2, 31))) elif rt > (math.pow(2, 31)-1) : return int(math.pow(2, 31)-1) else : return rt |
优化后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ #去掉前面的空白符 str = str.strip() if len(str) == 0 : return 0 flag = 1 if str[0] == '-': str = str[1:] flag = -1 elif str[0] == '+': str = str[1:] rt = 0 for i in str : if i >= '0' and i <= '9' : #ord函数,根据字符返回对于ASCII rt = 10*rt + ord(i) - ord('0') else : break rt *= flag if rt < -(math.pow(2, 31)) : return int(-(math.pow(2, 31))) elif rt > (math.pow(2, 31)-1) : return int(math.pow(2, 31)-1) return rt |
勉强看起来不错了…不过记得[:]很慢,可以试图优化下。
str.strip() 函数,去掉字符串前面的空白符
ord() 函数,根据字符返回对于ASCII
然后又更新了一下: 速度到了62ms
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ #去掉前面的空白符 str = str.strip() if len(str) == 0 : return 0 flag = 1 rt = 0 if str[0] in '+-' : if str[0] == '-': flag = -1 for i in range(1,len(str)) : if str[i] >= '0' and str[i] <= '9' : rt = 10*rt + ord(str[i]) - 48 else : break else : for i in range(len(str)) : if str[i] >= '0' and str[i] <= '9' : rt = 10*rt + ord(str[i]) - 48 else : break rt *= flag if rt < -2147483648 : return -2147483648 elif rt > (2147483647) : return 2147483647 return rt |
这样看起来,好像 in range 比较快一些…
更快的方法…想不到了(⊙﹏⊙)b…