leetcode String to Integer (atoi) python
2015-11-10 21:12
776 查看
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
相关文章推荐
- Python 安装 模块/Package
- 关于Python中深拷贝与浅拷贝的理解(二)---排bug
- Python windows7 install gevent
- Python调用C程序
- wxPython笔记(Getting started with wxPython 1)
- 关于Python中深拷贝与浅拷贝的理解(一)---概念
- python核心编程:repr()与str()的区别
- Python 之简单线程池创建
- python表达式操作符与数学函数
- python的模块
- python: 使用matplotlib的pyplot绘制图表
- LeetCode----Remove Duplicates from Sorted Array
- Python单元测试框架-unittest
- perl和python各自擅长什么领域?
- [LeetCode]题解(python):050-Pow(x, n)
- Python: sort,sorted,OrderedDict的用法
- python implement for selected problems
- [LeetCode]题解(python):049-Group Anagrams
- python基础-内置数据类型
- Python基础教程100例 练习2