题目来源:

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. php环境配置优化

    Php相关配置 – 基础 max_execution_time = 30 max_input_time = 60 memory_limit = 128 Mmax_input_vars = 1000 r ...

  2. Speex manul中文版

    Speex manul中文版   在VOIP的音频算法中,回音处理已经成为一个关系通话质量的主要问题. 回声的产生在IP网络主要有两种:1.声学回声2.电路回声 声学回声主要又分成以下几种:a ) 直 ...

  3. 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET

    让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...

  4. localstroge可以在页面间传递数值;

    连接地址为:http://4.suancai.sinaapp.com/localstorg/a.html 原理是,a页面设置了sessionstorge,b页面可以访问到; 并且已关闭浏览器,sest ...

  5. Extjs4 类的定义和扩展

    一般定义方式,注意方法和函数的添加方式不同.(添加函数只能用override方式添加不知为什么,有知道的,请搞之.) 定义一个类,并给他一个方法 1: Ext.define('Simple.Class ...

  6. Java中byte转int的方法

    byte转化为int有两种情况: 1)要保持数值不变 应用场景:数值计算.等等. 方法:能够直接採用强制类型转换:int i = (int) aByte, 比如:若aByte=0xff(即数值为-1) ...

  7. c++11新特性(4) lambda捕捉块

    lambda表达式中的方括号成为捕捉块,能够在这里指定怎样从所在的作用域中捕捉变量. 捕捉的意思是指能够在该lambda中使用该变量.即能够捕获外部变量在lambda表达式内使用. 能够使用两种方式来 ...

  8. css版hover现边框

    需要注意的是  hover中要给盒子加:position:relative; <style type="text/css"> *{margin:0;padding:0; ...

  9. 经典mssql语句大全

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  10. mysql xtrabackup 备份恢复实现,mysql命令备份数据库,打包压缩数据库

    简介 Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具.特点: (1)备份过程快速.可靠 ...