[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 ...
随机推荐
- Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh
Hyperpolyglot Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh grammar | quoting and escaping | charactersvar ...
- HDU 3501 Calculation 2
题目大意:求小于n的与n不互质的数的和. 题解:首先欧拉函数可以求出小于n的与n互质的数的个数,然后我们可以发现这样一个性质,当x与n互质时,n-x与n互质,那么所有小于n与n互质的数总是可以两两配对 ...
- ural 1057(数位dp)
数位dp题,关键是用树的思维去考虑. 对于一个数字X,要是能表示成K个B的不同次幂,等价于X在B进制下有且只有K个位上面的数字为一,其他位上的数字都为0. 具体读者可以去参考,国家集训队李聪的论文,里 ...
- UVA-548Tree(二叉树的递归遍历)
Tree Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Des ...
- js动画学习(五)
九.多属性同时运动 前面的例子都是每个属性单独运动,如果想要多属性同时运动怎么办?比如,我想要一个div的onmouseover事件中宽和高同时变化.下面这个函数是单独变宽: window.onloa ...
- java web分享ppt大纲 -- servlet包
概述 servlet相关代码所在包: javax.servlet javax.servlet.http 常用的功能模块 Servlet,filter,listener 常用的相关类: servlet ...
- iOS面试题04-runtime
runtime/KVO等面试题 1.KVO内部实现原则 回答:1>KVO是基于runtime机制实现的 2>当某个类的对象第一次被观察时,系统就会在运行期动态地创建该类的一个派生类,在这个 ...
- C++读写文件的简单例子
#include <iostream> #include <fstream> using namespace std; void main() { ofstream in; i ...
- [Jobdu] 题目1367:二叉搜索树的后序遍历序列
题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 输入: 每个测试案例包括2行: 第一行为1个整数 ...
- C#读取网页
public bool getweb(string strURL,out string buf) { buf=""; try { //Uri url=new Uri(strURL, ...