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. python 利用已有Ner模型进行数据清洗合并

    # -*- coding: utf-8 -*- from kashgari.corpus import DataReader import re from tqdm import tqdm def c ...

  2. thinkphp漏洞集合

    整合了一个集合,方便查询 thinkphp 5.0.22 1.http://192.168.1.1/thinkphp/public/?s=.|think\config/get&name=dat ...

  3. HashMap并发分析

    我们听过并发情况下的HashMap,会出现成环的情况,现在,我就来总结一下它成环的过程. 一言以蔽之,就是他在resize的时候,会改变元素的next指针. 之前在一篇博客里提到,HashMap的re ...

  4. docker私有仓库registry的使用

    1.registry的安装 关于docker registry的安装,可以说简单的不能再简单了,docker run一个容器就好了,也就是一条命令的事 docker run -d -p : --res ...

  5. 使用GDB和GEF进行调试

    使用GDB进行调试 这是编译ARM二进制文件和使用GDB进行基本调试的简单介绍.在您按照教程进行操作时,您可能需要按照自己的习惯使用ARM程序集.在这种情况下,你要么需要一个备用的ARM设备,或者你只 ...

  6. 【基础操作】博弈论 / SG 函数详解

    博弈死我了……(话说哪个小学生会玩博弈论提到的这类弱智游戏,还取石子) 先推荐两个文章链接:浅谈算法——博弈论(从零开始的博弈论) 博弈论相关知识及其应用 This article was updat ...

  7. TIOBE 2017 8月编程语言排行榜 后院“硝烟四起”

    处于排名榜最前面的几个编程语言的分数长期以来一直都在下降:Java和C在TIOBE榜单中的分数一直比较低.而且几乎所有其他排名前十的语言每年都在下降. 那么哪个什么语言抓住了这个机遇呢?这发生在排行榜 ...

  8. vim快速到行尾

    快速到行尾A,或者End键(挨着Home键) 快速到第一行gg 快速到行首Home键,数字键的上面

  9. MySQL第二次安装随笔

    找到之前的MySQL的安装包,重新安装MySQL. 1.设置环境变量,win10的可以右键此电脑-属性,在系统变量Path中添加mysql文件bin的路径 2.修改配置文件mydefault.ini( ...

  10. C#中无边框窗体移动或拖控件移动窗体

    [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("use ...