class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ intMax=2147483647 intMin=-2147483648 str=str.strip() strLen = len(str) if strLen <= 0: return 0 bolNegative = False tmp = str[0] start=0 if tmp == ‘-‘ or tmp == ‘+‘: start=1 if tmp == ‘-‘: bolNegative = True rs=0 for i in range(start, strLen ): tmp = str[i] if tmp >= ‘0‘ and tmp <= ‘9‘: rs=rs*10+int(tmp) elif tmp == ‘-‘ or tmp == ‘+‘: return 0 else: break intTmp = rs if bolNegative: intTmp*=-1 if intTmp > intMax: return intMax if intTmp < intMin: return intMin return intTmp
leetcode String to Integer (atoi) python
原文:http://www.cnblogs.com/allenhaozi/p/4954370.html