题目来源:

https://leetcode.com/problems/string-to-integer-atoi/


题意分析:

这道题也是简单题,题目意思是要将字符串转化成int。比如‘123’转成123.


题目思路:

由于有一些其他的输入直接用int()函数肯定是不可以的。比如说‘123b’用int()函数肯定是报错的。那么我们可以用一个ans = 0来初始化得到的int,从第一个字符s开始判断,得到新的ans是ans = ans*10 + int(s)。题目要注意的是一些特殊情况:

1.刚开始的空格,字符开始的空格字符忽略不算;

2.‘-’和‘+’字符,第一次出现的这两个字符可以代表得到的int的正负;

3.上述情况以外的所有非‘0’-‘9’的字符,出现这些字符的时候等于出现结束符;

4.得到的ans超过32位int最大长度。

只要在代码中加上几个bool判断符,字符的一些比较和ans的大小比较一下,答案就出来了。


代码(python):

 class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
size = len(str)
if size == 0:
return 0
ans = 0
b = True
b1 = True
positive = True
i = 0
while i < size:
if str[i] != ' ':
b1 = False
if str[i] == ' ' and b1:
i += 1
continue
if b:
if str[i] =='-':
positive = False
i += 1
b = False
continue
if str[i] == '+':
i += 1
b = False
continue
if str[i]>= '' and str[i] <= '':
ans = ans*10 + int(str[i])
if ans > 2147483647 and positive:
return 2147483647
if ans > 2147483648 and not positive:
return - 2147483648
else:
break
i += 1
if positive:
return ans
return -1 * ans

转载请注明出处:http://www.cnblogs.com/chruny/p/4801655.html

[LeetCode]题解(python):008-String to Integer (atoi)的更多相关文章

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

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

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

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

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

  4. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  5. LeetCode【8】. String to Integer (atoi) --java实现

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

  6. 【LeetCode】008. String to Integer (atoi)

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

  7. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

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

  8. [Leetcode]008.String to Integer (atoi)

    public class Solution { public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. 边界条 ...

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

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

  10. [leetcode]经典算法题- String to Integer (atoi)

    题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...

随机推荐

  1. 自己动手写RTP服务器——用RTP协议传输TS流

    上一篇文章我们介绍了关于RTP协议的知识,那么我们现在就自己写一个简单的传输TS流媒体的RTP服务器吧. 预备知识 关于TS流的格式:TS流封装的具体格式请参考文档ISO/IEC 13818-1.这里 ...

  2. .Net时间计算函数,统计某一天是一年的第几周,这一周从哪天开始到哪天结束

    /// <summary> /// 计算某年第一周的天数         /// </summary>         /// <param name="dt& ...

  3. Device Mapper Multipath(DM-Multipath)

    Device Mapper Multipath(DM-Multipath)能够将server节点和存储阵列之间的多条I/O链路配置为一个单独的设备.这些I/O链路是由不同的线缆.交换机.控制器组成的S ...

  4. js 触摸事件

    js触摸事件 应用在移动端 webkit内核都支持. 触摸事件api https://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html 事件 ...

  5. Foundation 框架 NSFileManager,NSData 简单的文件操作

    一.简单展示NSFileManager的使用 #import <Foundation/Foundation.h> int main(int argc, const char * argv[ ...

  6. Python 3 学习笔记2

    教程链接:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 高级特性 切片 取一个l ...

  7. MSSQL 修改数据库的排序规则

    1.修改数据库排序规则 ALTER DATABASE [CHARACTER] COLLATE Chinese_PRC_CI_AS ; 2.修改表中列的排序规则 如果下列其中之一当前正在引用一个列,则无 ...

  8. OpenGL: 环境配置和图元的绘制

    前言 距离上一篇博客已经过去一个半月了,这段时间过得确实充实,虽然一大段时间泡在图书馆复习,但至少也能学到点东西.跨年晚和元旦一整天,全身心投入图形学小课设的编程,终于实现了老师要求的所有功能,回想起 ...

  9. 百度下载google 浏览器安装失败

    installer integrity check has failed. Common causes include incomplete download and damaged media co ...

  10. CDOJ 1259 昊昊爱运动 II bitset+线段树

    题目链接 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r天参加了 ...