mycode  98.26%

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

  1. class Solution(object):
  2. def myAtoi(self, str):
  3. """
  4. :type str: str
  5. :rtype: int
  6. """
  7. str = str.strip()
  8. if not str :
  9. return 0
  10. res , start = '' , 0
  11. if str[0] == '-' or str[0] == '+' :
  12. res = str[0]
  13. start = 1
  14. if start >= len(str) or not str[start].isnumeric() :
  15. return 0
  16. while start < len(str) and str[start].isnumeric():
  17. res += str[start]
  18. start += 1
  19. res = int(res)
  20. if res > 2147483647 :
  21. res = 2147483647
  22. elif res < -2147483648:
  23. res = -2147483648
  24. return res

参考

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

  1. def myAtoi(self, S):
  2. """
  3. :type str: str
  4. :rtype: int
  5. """
  6. res = ''
  7.  
  8. S1 = S.strip() # need to know
  9.  
  10. for s in S1:
  11. if s == '': continue
  12. if res != '' and s in '+-': break
  13. if s in '-+0123456789': # need to know
  14. res += s
  15. else:
  16. break
  17.  
  18. if res == '' or res == '+' or res== '-':
  19. return 0
  20. elif int(res) < -2**31:
  21. return -2**31
  22. elif int(res) > (2**31)-1:
  23. return (2**31) -1
  24. else:
  25. 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. 94. Binary Tree Inorder Traversal (Java)

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  2. 获取图书isbn信息 共享图书开发 图书信息接口开发过程中的心得体会

    最近做一个图书共享的项目,需要用户扫一扫书籍后面的一维码,获取到书籍的isbn号码,然后通过这个isbn号码能够直接获取到这本书的名字.简介.价格.图片等信息. 于是百度搜了下,之前很多的豆瓣的接口, ...

  3. 【Groovy】 Groovy笔记

    一.简单了解Groovy Groovy简介: Groovy是基于JVM的敏捷开发语言,语法与Java类似,但更加简洁,容错性也比Java强,同时Java能非常好的契合(例如Groovy能够使用Java ...

  4. IIS7发布asp.net mvc提示404

    之前服务器用的都是2003Server的服务器,发布mvc项目都没问题,今天换了一台机器,系统为Windows Server2008 R2  64位的发布mvc项目后就提示: 百度看到好多人说在web ...

  5. 一、Nginx多站点配置

    一.下载 目录文件: 二.运行方式 (1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过 (2)打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start ...

  6. Codeforces Round #581 (Div. 2)A BowWow and the Timetable (思维)

    A. BowWow and the Timetable time limit per test1 second memory limit per test256 megabytes inputstan ...

  7. pidstat 命令详解(转载)

    转自https://www.jianshu.com/p/3991c0dba094 pidstat 概述 pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu.内存.线程.设备I ...

  8. python file对象测试数据的读写操作及OS模块介绍(四)

    import   from....import 引入模块 引入类 ①import 如果文件在lib下而且是python模块 :import 模块名. ②from....import from 包名.包 ...

  9. 二进制数组-ArrayBuffer对象

    ArrayBuffer对象:存储二进制数据的一段内存,不能写/读 ,类似数组的对象 只能通过TypedArray视图/DataView视图 读/写 va buf = new ArrayBuffer(3 ...

  10. Attention机制中权重的计算

    Attention mechanism中,给输入序列中对应的每一个Ht分配权重(打分)究竟是如何打分? 输入序列打分,a(s, h) 其中s是输出序列的t-1时刻的隐藏层状态,h是输入的多个状态,