首页 > 编程语言 > 详细

leetcode String to Integer (atoi) python

时间:2015-11-10 22:22:15      阅读:217      评论:0      收藏:0      [点我收藏+]
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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!