我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/

008. String to Integer (atoi) [E]


题目

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

思路

这题也比较好做,关键是要考虑挺多东西,我也是提交了好多次才发现有这么多要考虑的地方。

  • 开头的空格
  • 正负符号的处理
  • 溢出处理
  • 非法输入

开头空格处理:

while(str[i] == " ") i++;

正负号的处理:我觉得yuruofeifei这个解决方案简直赞

if (str[i] == '-' || str[i] == '+') {
sign = 1 - 2 * (str[i++] == '-');
}
……
return base * sign;

溢出处理(可以参考上一道题):

if (base >  INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > INT_MAX%10)) {
if (sign == 1) return INT_MAX;
else return INT_MIN;
}

非法输入:其实只用过滤就行了

 while (str[i] >= '0' && str[i] <= '9') {
……
}

代码

我的代码,不够简洁,可以参考yuruofeifei的代码,在下面

class Solution {
public:
int myAtoi(string str) {
long tmp=0;
bool neg;
int i = 0;
while(str[i] == ' ') i++; //读掉空格
neg = str[i] == '-'?1:0;
for(i = i+ (neg || str[i] == '+');i < str.length();i++) //如果是- 或 + i+1跳过符号
{
if(str[i] - '0' >= 0 && str[i] - '0' < 10) //过滤非法输入
{
tmp *= 10;
tmp += (str[i] - '0');
if(tmp >= INT_MAX && !neg) //溢出判断
{
tmp = INT_MAX;
break;
}
if(tmp -1 >= INT_MAX && neg) //除了符号,INT_MAX和INT_MIN只差1
{
tmp = INT_MIN;
break;
}
}
else break;
}
if(neg) return -tmp;
return tmp;
}
};

yuruofeifei的代码

int myAtoi(string str) {
int sign = 1, base = 0, i = 0;
while (str[i] == ' ') { i++; }
if (str[i] == '-' || str[i] == '+') {
sign = 1 - 2 * (str[i++] == '-');
}
while (str[i] >= '0' && str[i] <= '9') {
if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) {
if (sign == 1) return INT_MAX;
else return INT_MIN;
}
base = 10 * base + (str[i++] - '0');
}
return base * sign;
}

《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理的更多相关文章

  1. LeetCode题解 #8 String to Integer (atoi)

    又是一道恶心的简单题. 一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了. 考虑不周到只能一个个补了. 列举一下恶心的case / ...

  2. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

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

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

  4. LeetCode(8) - String to Integer (atoi)

    虽然是easy,却是比较繁琐的一道题,需要考虑各种边界条件.在WA了好几遍之后,才把各种边界条件给补全.需要考虑到的因素如下: 输入不合法字符,非"0-9",对于首位,合法字符还包 ...

  5. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  6. 【LeetCode】8. String to Integer (atoi) 字符串转整数

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

  7. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  8. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

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

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

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

随机推荐

  1. 23 DesignPatterns学习笔记:C++语言实现 --- 1.5 Prototype

    23 DesignPatterns学习笔记:C++语言实现 --- 1.5 Prototype 2016-07-21 (www.cnblogs.com/icmzn) 模式理解

  2. phonegap/cordova学习建议

    在技术群里面,一直有一些新人进来,问了一些让人可笑不得的问题.国内的资料相对比较少,而且很旧,都是一些2.X版本的资料.因此想写一些东西,帮助一下新人,让他们少走弯路. 首先说一些很多人问的问题,个人 ...

  3. hdu2364之BFS

    Escape Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. Websocket出现的错误

    前端使用sockjs,后台使用spring的websocket框架 结果在一个网络较慢的地方,发现tomcat报错信息: Oct 28, 2015 10:10:43 AM org.apache.cat ...

  5. Java泛型学习笔记

    泛型是Java5引进的新特征,是类和接口的一种拓展机制,主要实现参数化类型机制.Java的泛型,跟C++的类模板有很多相似的地方,或者说,就是C++类模板的升级版. 泛型类 在开发过程中,我们或许要设 ...

  6. Xshell传输文件

    用rz,sz命令在xshell传输文件 很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz 首先你的Ubuntu需要安装rz.sz(如果没有安装请执 ...

  7. TSQL--TOP选项

    TOP选项需要依据ORDER来选取记录,可以依据行数和百分比来选取记录 按照行数来选取10行记录 SELECT TOP(10) * FROM T1 ORDER BY ID 按照行数来选取10%的记录 ...

  8. c# 利用t4模板,自动生成Model类

    我们在用ORM(比如dapper)的时候,很多时候都需要自己写Model层(当然也有很多orm框架自带了这种功能,比如ef),特别是表里字段比较多的时候,一个Model要写半天,而且Model如果用于 ...

  9. Android intent 传值不更新的原因和解决办法

    当 Activity 的启动模式是 singleTask 或者 singleInstance 的时候.如果使用了 intent 传值,则可能出现 intent 的值无法更新的问题.也就是说每次 int ...

  10. Linux 历史信息history显示执行时间

    fc命令 fc命令自动掉用vi编辑器修改已有历史命令,当保存时立即执行修改后的命令,也可以用来显示历史命令.fc命令编辑历史命令时,会自动调用vi编辑器.fc保存文件后,会自动执行所编辑过的命令. 测 ...