mycode  98.26%

易错点: while循环式,and判断的地方先判断下标会不会超出范围

class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
str = str.strip()
if not str :
return 0
res , start = '' , 0
if str[0] == '-' or str[0] == '+' :
res = str[0]
start = 1
if start >= len(str) or not str[start].isnumeric() :
return 0
while start < len(str) and str[start].isnumeric():
res += str[start]
start += 1
res = int(res)
if res > 2147483647 :
res = 2147483647
elif res < -2147483648:
res = -2147483648
return res

参考

思路:哪些情况下可以直接返回0呢 1) 空 2)+-符号不是开头3) 遍历到的字符不是空格、+-、数字

 def myAtoi(self, S):
"""
:type str: str
:rtype: int
"""
res = '' S1 = S.strip() # need to know for s in S1:
if s == '': continue
if res != '' and s in '+-': break
if s in '-+0123456789': # need to know
res += s
else:
break if res == '' or res == '+' or res== '-':
return 0
elif int(res) < -2**31:
return -2**31
elif int(res) > (2**31)-1:
return (2**31) -1
else:
return int(res)

leetcode-easy-string- 8 String to Integer (atoi)的更多相关文章

  1. 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 ...

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

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

  3. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

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

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

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

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

  6. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  7. 【leetcode】String to Integer (atoi)

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

  8. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  9. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  10. leetcode第八题--String to Integer (atoi)

    Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...

随机推荐

  1. Postman简单的接口测试

    DownloadPostmanApphttps://www.getpostman.com/downloads/ https://www.getpostman.com/downloads/canary ...

  2. 关于fastJson的几个问题

    1.将对象中为null的属性也给序列化出来 可以使用SerializaerFeature实现 JSON.toJSONString(gas, SerializerFeature.WriteMapNull ...

  3. 安装Maatwebsite \ EXCEL \ ExcelServiceProvider

    安装时报错 composer You can also run `php --ini` inside terminal to see which files are used by PHP in CL ...

  4. Linux 查看主机、CPU、内存、内核、网卡或MAC地址、关机、重启、当前使用人、网络连接状态、主机目前使用状态

    7 uname -a 显示主机名.内核.硬件结构等全部信息 unmae -r 只显示内核 查看Redhat和centos的内核版本也可以用cat /etc/redhat-release 或cat /e ...

  5. Mongodb操作-更新操作符

    1.$inc 用法:{$inc:{field:value}} 作用:对一个数字字段的某个field增加value 示例:将name为chenzhou的学生的age增加5 > db.student ...

  6. 如何在当前目录下打开Windows cmd?

    在当前目录下,按Alt + D (全选当前目录),然后输入 cmd 再按回车 Enter .

  7. git中working tree, index, commit

    这三个名字可以简单理解为文件在本地仓库存在的三种不同的位置. 如下,是做commit提交两段提交过程,工作区(working tree),暂存区(index)和 branch(commit). wor ...

  8. SpringMVC优雅的获取HttpSevletRequest及HttpServletResponse简录

    https://cloud.tencent.com/developer/article/1403947 通常情况下,SpringMVC可以通过入参的方式绑定HttpServletRequest和Htt ...

  9. Linux下普通用户与root用户之间的互相切换

    只是切换root身份,环境仍是普通用户shell su.su -.su root 按照提示输入相应的root密码,就可登录到root权限下. 用户和shell环境都切换为root sudo -i.su ...

  10. vue 设置当前页背景色

    beforeRouteEnter(to, from, next) { // 添加背景色 document.querySelector('body').setAttribute('style', 'ba ...