我现在在做一个叫《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. Google Tango service outdated谷歌Tango的服务过时了

    If you device showed "tango service outdated." It means that your Tango Core need to be up ...

  2. 安装TestNG在eclipse中的插件

    两种方法可以安装TestNG Eclipse插件: 第一种,离线安装 TestNG Eclipse插件下载地址http://testng.org/doc/download.html. 下载下来以后,放 ...

  3. solr特点七:Plugins(扩展点)

    http://wiki.apache.org/solr/SolrPlugins 在 Solr 1.3 中,扩展 Solr 以及配置和重新整理扩展变得十分简单.以前,您需要编写一个 SolrReques ...

  4. 配置sql server 允许远程连接

    如果要想远程连接数据库那么则需要在一个局域网中或一个路由器中才可以做到 接下来就是具体的操作检查sqlserver数据库是否允许远程连接 具体操作为 (1)打开数据库,用本地帐户登录,右击第一个选项, ...

  5. django drf 级联数据和RetrieveModelMixin

    1.定义View from django.shortcuts import render from rest_framework.views import APIView from rest_fram ...

  6. ASP.NET MVC学习笔记(二)登陆验证

    书上的验证时在配置文件中直接声明用户名和密码,想改成从数据验证账号和密码,搞了一下午都没高出来,不断的调试,发现 var table = userInfo.Tables.FirstOrDefault( ...

  7. linux下使用supervisor启动.net core mvc website的配置

    发布好的asp.net core mvc项目, 如果想在window或linux下的以控制台程序启动的话,可以用下面的命令 dotnet MyProject.dll --urls="http ...

  8. Programmatically Disable Event Firing on List Item Update in SharePoint 2010

    1. Microsoft.SharePoint.dll Create EventFiring.cs 1.Right-click on the project, select Add and click ...

  9. brew安装指定版本boost

    brew 如何安装指定版本的boost brew uninstall boost brew install boost@1.57 brew link boost@1.57 --force --over ...

  10. Vue的组件和路由

    组件 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能. 组件的 ...