《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

来源:互联网 发布:js把URL转换为对象 编辑:程序博客网 时间:2024/06/18 13:39

我现在在做一个叫《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;}
0 0