每日算法之八:String to Integer (atoi) 及溢出分析

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.

要求就是把字符串转化为整形,按照我们的理解就可以逐字符遍历,转化为整形即可,比如字符串"123454",我们只要取出第零个字符‘1‘-‘0’就可以得到数字1,然后把1乘以10,再加上‘2’-‘0’·····这样依次就可以得到转化之后的整形数字。当然有几个地方需要注意:

1)字符串可能以正负号开始,也可能包含一些其他的非数字型字符,是不能转化为数字的,是忽略还是报错看需求

2)越界,字符串转化到整形数字之后的数字串可能超出表示范围,超出的可以置为INT_MAX或者INT_MIN。

代码参考如下:

class Solution {
public:
    int atoi(const char *str) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        long long ret = 0;//存储最终转化的数字

        const char *p = str;

        while(*p == ‘ ‘) p++;//忽略空格

        bool valid = true;
        while(valid && *p == ‘+‘) {
            valid = false;
            p++;
        }

        while(*p == ‘0‘) p++;

        bool minus = false;
        if(*p == ‘-‘){
            minus = true;
            p++;
        }

        while(*p != ‘/0‘){
            if(*p >= ‘0‘ && *p <=‘9‘){
                ret = ret * 10 + *p - ‘0‘;
                if(!minus && ret > INT_MAX) return INT_MAX; // positive and overflow
                if(minus && -ret < INT_MIN) return INT_MIN; // negative and overflow
                p++;
            } else {        // no digit then break
                break;
            }
        }

        return minus ? -ret : ret;
    }
};

这里需要特别注意的就是怎么预防溢出,如果都是int类型,用ret+1>INT_MAX进行判断是错误的,当ret是INT_MAX时,再进行加一操作是会溢出的,变为最小的负数,对计算机而言对符号性的整数是进行位运算的,即对INT_MAX:0111 1111 1111 1111 1111 1111 1111 1111 进行加1之后就变为

1000 0000 0000 0000 0000 0000 0000 0000也就是INT_MIN。实质上产生的进位到了符号位。上面能AC的原因是使用了long long (8字节)类型的整形存储int(4字节)。所以能够判断大于INT_MAX,现在假定形参也是longlong类型或者不允许使用刚才的方式,那么我们应该怎么做呢?

#include <limits.h>
void f(signed int si_a, signed int si_b) {
signed int sum;
if (((si_b > 0) && (si_a > (INT_MAX - si_b))) ||
((si_b < 0) && (si_a < (INT_MIN - si_b)))) {
/* Handle error */
return;
}
sum = si_a + si_b;
}

我们使用这种方法就可以进行溢出的判断,这是苹果安全编码的建议,应该是可用高效的。

因为溢出导致的异常是常见并且是难以发现的,因此一定要注意。

每日算法之八:String to Integer (atoi) 及溢出分析,布布扣,bubuko.com

时间: 2024-10-04 12:35:41

每日算法之八:String to Integer (atoi) 及溢出分析的相关文章

[leetcode]经典算法题- String to Integer (atoi)

题目描述: 把字符串转化为整数值 原文描述: 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

[算法练习]String to Integer (atoi)

题目说明: 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

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

题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 class Solution { 2 fun myAtoi(str: String): Int { 3 val maxInt = "2147483647" 4 val maxIntS = "+2147483647" 5 val minIntS = "-21474836

每日算法之十一:Integer to Roman

题目:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 罗马表示方式如下: I = 1; V = 5; X = 10; L = 50; C = 100; D = 500; M = 1000; 其中每两个阶段的之间有一个减法的表示,比如900=CM, C写在M前面表示M-C. 范围给到3999,感觉情况不多直接打表其实更快,用代码判断

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

【leetcode】String to Integer (atoi)

String to Integer (atoi) 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 f

[LeetCode][JavaScript]String to Integer (atoi)

String to Integer (atoi) 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 f

LeetCode【8】. String to Integer (atoi) --java实现

String to Integer (atoi) 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 f