题目来源:

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. 将 SQL Server 实例设置为自动启动(SQL Server 配置管理器)

    本主题说明如何使用 SQL Server 配置管理器在 SQL Server 2012 中将 SQL Server 实例设置为自动启动. 在安装过程中,SQL Server 通常配置为自动启动. 如果 ...

  2. webp 初探

    WebP是Google新推出的影像技术,它可让网页图档有效进行压缩,同时又不影响图片格式兼容与实际清晰度,进而让整体网页下载速度加快. 如果我们能将其应用在现有的图片上,将可以进一步减少图片大小加快页 ...

  3. python import 自己的包

    在写python时,有时候写的一个python文件可能需要被其他python文件所用,那么可以用导入包 import 的 方式: 1.自己写的包放到哪里? >>> import sy ...

  4. java的表达式

    Java是面向表达式的语言,Java中一个简单表达式可以是下面任意一种: ● 常量:7.false.● 单引号括起来的字符字面常量:'A'.'3'.● 双引号括起来的字符串字面常量:"foo ...

  5. ubuntu12.04&15.04 安装lamp(12.04为主)

    ubuntu 12.04&15.04下安装lamp环境 注意:如果是ubuntu15.04下,apache2.4.10的话,直接在/etc/apache2/apache2.conf文件的后边直 ...

  6. SDOI(队列)

    SDOI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Sub ...

  7. Echoprint系列--Android编译与调用

    在Echoprint系列--编译中编译了源代码,这次将Echoprint移植到Android平台并測试识别歌曲功能. 一.编译库 1.环境准备 Android NDK,我的是android-ndk-r ...

  8. java concurrent之前戏synchronized

    对于多线程共享资源的情况须要进行同步,以避免一个线程的修改被还有一个线程的修改所覆盖. 最普遍的同步方式就是synchronized. 把代码声明为synchronized.有两个重要后果,一般是指该 ...

  9. linux核心之进程管理

    进程就是处于执行期的程序(目标码存放在某中介质上).进程并不仅仅局限于一段可执行程序代码,通常还包括其他资源,例如打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内存映射的内存地址空间 ...

  10. 腾讯云部署Flask应用

    由于新浪云现在不免费了.而且云豆也用完了.所以去腾讯云申请了个学生云主机,一元一个月. 不过部署开发环境还是有点麻烦的,搞了好几天,终于部署成功了! 下面说部署过程: 我云主机用的是 Ubuntu 1 ...