[LeetCode]题解(python):008-String to Integer (atoi)
题目来源:
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)的更多相关文章
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Leetcode]008.String to Integer (atoi)
public class Solution { public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. 边界条 ...
- leetcode第八题--String to Integer (atoi)
Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...
- [leetcode]经典算法题- String to Integer (atoi)
题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...
随机推荐
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2002 [题目大意] 给出一片森林,操作允许更改一个节点的父亲,查询一个节点的深度. [ ...
- Java知识点复习
总结下java的知识点 final 关键字-方法:不能被子类重写(override)-变量:不能被修改-类:不可以被继承,派生子类 finally 关键字与try/catch语句配合使用,即使有异常抛 ...
- nodejs服务端开发学习笔记
正在学习中,不断改错... 学习了一段时间nodejs,对其中的很多东西还不是很理解,在网上看过很多的例子,希望通过自己的一些总结让自己了解的更全面些,同时也作为学习笔记留存备忘. 准备工作 node ...
- unix more命令
[语法]: more [-cdflrsuw] [- 行数] [+ 行数] [+ / 模式 ] [ 文件 ... ] [说明]: 将文件显示在终端上.每次一屏,在左下部显示 --more--.若是 ...
- java concurrent之前戏synchronized
对于多线程共享资源的情况须要进行同步,以避免一个线程的修改被还有一个线程的修改所覆盖. 最普遍的同步方式就是synchronized. 把代码声明为synchronized.有两个重要后果,一般是指该 ...
- Objective-C分类 (category)和扩展(Extension)
1.分类(category) 使用Object-C中的分类,是一种编译时的手段,允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以 ...
- 关于iOS8上本地通知接收不到的问题
在iOS8系统开发使用本地通知时,会出现如下的相关提示语: 1 Attempting to schedule a local notification2 with an alert but haven ...
- JS提取URL中的参数
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- poj 3259Wormholes (spfa最短路径)
#include<stdio.h> #include<string.h> #include<limits.h> #include<queue> usin ...
- ECharts-百度地图使用
Demo可以直接搜到 这里主要是拖js