您的位置:首页 > 其它

leetcode 8. String to Integer (atoi)

2018-03-16 00:47 393 查看
本题目是medium,根据题目要求,考虑的情况比较多,难点是一次想全所有情况,算法上没有什么大难度

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. Requirements for 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.Solution:
class Solution(object):
    def myAtoi(self, stri):
        """
        :type str: str
        :rtype: int
        """
        if not stri:
            return 0
        #去掉空白
        
        while stri[0] == " ":
            if len(stri) == 1:
                return 0
            else:
                stri = stri[1:]
        
        if len(stri) == 1 and self.judgeNum(stri[0]):
            return int(stri)
        elif len(stri) == 1:
            return 0
        else:
            flag = True
            start = 0
            if stri[0] == '-':
                flag = False
                start = 1
            elif stri[0] == '+':
                start = 1
            elif not self.judgeNum(stri[0]):
                return 0
            rtn = 0
            #正负最大最小值设置
            max_int = 2147483647
            min_int = 2147483648
            first = False
            for i in range(start,len(stri)):
                if self.judgeNum(stri[i]):
                    rtn = rtn*10 + int(stri[i])
                    first = True
                    if flag and rtn > max_int:
                        return max_int
                    if not flag and rtn > min_int:
                        return -min_int
                else:
                    if first:
                        return self.ret(flag,rtn)
                    else:
                        return 0
                
            return self.ret(flag,rtn)
            
    def ret(self,flag,rtn):
        if flag:
            return rtn
        else:
            return -rtn
    
    def judgeNum(self,char):
        #判断是否是数字
        if '0'<=char<='9':
            return True
        else:
            return False
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode