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. ubuntu - 如何以root身份使用图形界面管理文件?

    nautilus 是gnome的文件管理器,但是如果不是root账号下,权限受限,我们可以通过以下方式以root权限使用! 一,快捷键“ctrl+alt+t”,调出shell. 二,在shell中输入 ...

  2. 多线程编程-- part 5.2 JUC锁之Condition条件

    1.Condition介绍 Condition的作用是对锁进行更精确的控制.Condition中的await()方法相当于Object的wait()方法,Condition中的signal()方法相当 ...

  3. linux 之实现定时任务

    一.方式一 (1)命令行 的方法: 一.方式一 需求:每分钟执行一次/etc 目录的添加 到/tmp/a.txt 中 (1) touch a.txt创建文件 (2) crotab -e 进行任务的定制 ...

  4. 韦东山嵌入式Linux学习笔记08--中断体系结构

    中断是什么? 举个栗子, 系统怎么知道你什么时候插入鼠标这个设备? 可以有两种处理方式: 1. 查询方式: 轮询去检测是否有设备插入; 2. 中断的方式 当鼠标插入这个事件发生时, 置位某个寄存器,告 ...

  5. deep_learning_Function_tf.control_dependencies([])

    tf.control_dependencies([])函数含义及使用 2019.02.23 14:01:14字数 60阅读 420 tf.control_dependencies([controls_ ...

  6. Mac OSX编译安装php5.6

    安装好OSX 10.13以后默认自带的php7.1.7,跟现有环境不兼容,所以准备编译安装php5.6,自带的php7不建议卸载,重新安装一份php5.6 1.安装php的一些依赖,推荐使用brew安 ...

  7. zencart根据configuration_id cID查找站点配置

    admin/configuration.php?gID=6&cID=1075 zencart根据configuration_id cID查找站点配置 ; zencart根据configurat ...

  8. local_time

    time_t time(time_t *tloc); 功能:获取纪元1970-01-01 00:00:00以来所经历的秒数 参数: tloc:用来存储返回时间 返回值:成功:返回秒数, 失败:-1 - ...

  9. ora 12518监听程序无法分发客户机连接

    首先修改ORACLE的PROCESS.SESSION数量 查看当前ORALCE PROCESS数量 SQL> show parameter process 查看当前ORALCE SESSION数 ...

  10. GitHub : Hello World

    这个写的太好啦,让我也没啥可以写的啦:http://www.open-open.com/lib/view/open1454507333214.html