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 >= '' and tmp <= '':
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的更多相关文章

  1. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  2. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  3. [Leetcode] String to integer atoi 字符串转换成整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  4. [LeetCode] String to Integer (atoi) 字符串

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  5. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  6. [LeetCode]String to Integer (atoi)

    题意:字符串转正数 原题来自:https://leetcode.com/problems/string-to-integer-atoi/ 分析: <程序员面试宝典>上出现的面试题,主要是考 ...

  7. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  8. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  9. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

随机推荐

  1. 使用扩展名获取mimetype

    在Android中很多时候我们需要计算出文件的mimetype,而我们通常的思路就是通过扩展名来获取对应的mimetype,而如果自行处理,将维护一个比较大的映射表,而实际上大可不必,Android提 ...

  2. Android常用控件之GridView使用BaseAdapter

    我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...

  3. Copy from chromium-dev!

    https://app.yinxiang.com/pub/gguangle0/chromium-dev 做了一些搬运工的活..............

  4. ASP.NET MVC 学习之路-1

    本文在于巩固基础 学习参考书籍:ASP.NET MVC4 Web编程 首先确定我们学习MVC的目标: 我们学习ASP.NET MVC的目的在于开发健壮的.可维护的Web应用,当然这需要一定的知识基础, ...

  5. struts2 模型驱动的action赋值优先顺序

    struts2 模型驱动的action赋值优先顺序: 1.优先设置model的属性. 2.如果model属性中没有对应的成员变量,则向上冒泡,寻找action中的属性进行set. 如果action中的 ...

  6. Node.js(转) -- 临时来说还看不懂!

    转自:http://blog.jobbole.com/53736/ 本文由 伯乐在线 - Lellansin 翻译.未经许可,禁止转载!英文出处:toptal.欢迎加入翻译组. 介绍 JavaScri ...

  7. Thinkphp利用微信多客服消息推送取货二维码消息

    首先看微信官方的说法: 当用户主动发消息给公众号的时候(包括发送信息.点击自定义菜单.订阅事件.扫描二维码事件.支付成功事件.用户维权), 微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改 ...

  8. Javascript基础示例:用JS写简易版贪吃蛇(面向对象)

    废话不多说,代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...

  9. struts2中#,$,%的用法以及el,ognl表达式的用法

    OGNL, JSTL, STRUTS2标签中符号#,$,%的用法示例 取Session中的值 <c:out value="${sessionScope.user.userId}&quo ...

  10. 2 kNN-K-Nearest Neighbors algorithm k邻近算法(二)

    2.3 示例:手写识别系统 2.3 .1 准备数据:将图像转换为测试向量 训练样本:trainingDigits 2000个例子,每个数字大约200个样本 测试数据:testDigits 大约900个 ...