StringToInt (atoi)-LeetCode

"1234ab" -> 1234

“1234   567” -> 1234

"     123" -> 123

"+-" -> invalid

"+1234" -> 1234

"-1234" -> 1234

"2147483648" -> 2147483647  正数越界返回INT_MAX,负数越界返回INT_MIN

思路:

1)考虑前面是一堆空格

2)只允许有一个 ‘+’ 或一个 ‘-’

3)往后查看,若不是数字就直接跳出,若是数字,一位一位res = res*10 + (str-'0')

4) 在乘法之前,要判断是否越界。

<gs id="19f93463-8595-4311-a1a8-09804cc741ba" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark">if</gs><gs id="48c42ff8-d75b-48ad-b844-6fc7a060b000" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark">(</gs>INT_MAX/10 < sum || (INT_MAX/10 == sum && INT_MAX%10 < (<gs id="e548ef5b-61d3-4f11-8a60-1a5957704855" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark">str</gs><gs id="f0825e87-25d7-4493-a282-0d5e19b78d28" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark">[</gs><gs id="a3ac5acf-179b-4b9b-b0da-6cd62671863b" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark">i</gs>] - '0'))<gs id="5c909068-aa85-4b56-8c5c-e06838d45105" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark">)</gs><gs id="86ce7c2d-d6d7-43f4-8243-92eb20c06a94" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark">{ </gs>return sign == 1<gs id="61c12eec-fe2f-424f-b143-e38ce28b6785" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark"> ?</gs> <gs id="51b37125-51fe-4d24-a069-0d543194e1a0" ginger_software_uiphraseguid="5ced6fb8-b2eb-43ce-a80c-4dfab62b25c6" class="GINGER_SOFTWARE_mark">IN</gs><gs id="fa2a21a7-33ad-4c33-a1ad-01e9255ec182" ginger_software_uiphraseguid="5ced6fb8-b2eb-43ce-a80c-4dfab62b25c6" class="GINGER_SOFTWARE_mark">T</gs>_MAX<gs id="c028dbcc-57f0-4a90-ad8c-ac47b7633a71" ginger_software_uiphraseguid="e63d797c-4616-4605-8a3c-8c8bb79f0c77" class="GINGER_SOFTWARE_mark"><gs id="4d0accd5-88f2-4217-9a8e-47cfb4237062" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark"> :</gs></gs> INT_MIN;<gs id="6b948d50-9eab-40bc-b626-624c7abf4f79" ginger_software_uiphraseguid="579d2ea9-fe98-4109-8904-bb508a7798e6" class="GINGER_SOFTWARE_mark"> }</gs> 

注意越界不能在之后判断,不能写成:

int last = res;
int cur = str[i]-'0';
res = res*10 + cur;
if(last != (res-cur)/10) return (sign == 1) ? INT_MAX: INT_MIN;

因为 res = 214748364+8 (越界) = -2147483648.

但是  (res-cur)/10 = (-2147483648 - 8)(越界= 2147483640)  / 10   居然又回来了,与last相等了!  

所以一定要在之前判断。


时间复杂度:

扫描一遍 O(n)


Code (C++):

class Solution {
public:
    int myAtoi(string str) {
        if(str.size() == 0) return 0;
        int res = 0;
        int sign = 1;
        int i = 0;
        while(i < str.size() && str[i]==' ') i++;
        if(str[i] == '+') 
        {
            sign = 1;
            i++;
        }
        else if(str[i] == '-') 
        {
            sign = -1;
            i++;
        }
        while(i<str.size())
        {
            if(!isValidNum(str[i])) break;
            int cur = str[i]-'0';
            if(INT_MAX/10 < res || (INT_MAX/10 == res && INT_MAX%10 < cur)) return (sign == 1) ? INT_MAX: INT_MIN;
            res = res*10 + cur;
            i++;
        }
        return res*sign;
    }
    bool isValidNum(char c)
    {
        if( c<='9' && c>='0') return true;
        return false;
    }
};





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值