题目
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.
实现
- class Solution:
- # @param {string} str
- # @return {integer}
- def myAtoi(self, str):
- INT_MAX=2147483647;INT_MIN=-2147483648
- index=0
- while index<len(str) and str[index]==' ':index+=1
- flag=1
- if index<len(str) and str[index]=='-':
- index+=1
- flag=-1
- elif index<len(str) and str[index]=='+':
- index+=1
- res=0
- while index<len(str):
- if str[index]<'0' or str[index]>'9':return flag*res
- digit=ord(str[index])-ord('0')
- if flag==1 and res*10+digit>INT_MAX:return INT_MAX
- if flag==-1 and res*10+digit>-INT_MIN:return INT_MIN
- res=res*10+digit
- index+=1
- return flag*res