【LeetCode】008 String to Integer (atoi)

题目:LeetCode 008 String to Integer

题意:完成内置函数atoi的功能,将字符串转换成整数。

教训:一开始理所应当的随便一写,然后发现有很多的异常情况需要处理。然后按照C++ Reference中关于atoi的规定一条一条写,才AC。另外还有一个溢出的问题,一开始以为int会自动处理直接返回边界值,其实不是,如果溢出的话大于2147483647的数会给变成负数,因此要单独判断是否会大,但是设置成longlong 之后出现的问题是,还有可能会溢出longlong,所以每次对ans进行改变之后都要判断是否在integer的范围之内。

atoi的函数规则:http://www.cplusplus.com/reference/cstdlib/atoi/

1、前导空格忽略

2、判断是否是以‘+‘、‘-‘为前导来规定整数的正负

3、在整数后面有非数字的字符则忽略返回前面的整数

4、如果无法构成有效整数,即含有其他乱七八糟字符,返回0

5、如果超出整数范围,大于的返回INT_MAX=2147483647,小于的返回INT_MIN=-2147483648.

代码如下:

class Solution {
public:
    int myAtoi(string str) {
        int len = str.size(), i = 0, flag = 1;
        const int MAX = 2147483647, MIN = -2147483648;
        long long ans = 0;

        // 可能有前导空格
        while(str[i] == ‘ ‘) i++;

        // 可能在前面有表示正负的符号
        if(str[i] == ‘-‘)
        {
            flag = -1;
            i++;
        }
        else if(str[i] == ‘+‘) i++;

        while(i < len)
        {
            // 保证加起来的字符为有效数字
            if(str[i] <= ‘9‘ && str[i] >= ‘0‘)
            {
                ans *= 10;
                ans += (str[i++]-‘0‘);
                if(ans*flag > MAX) return MAX;
                if(ans*flag <MIN) return MIN;
            }
            // 可能最后有其他字符
            else break;
        }
        ans *= flag;
        if(ans > MAX) return MAX;
        else if(ans <MIN) return MIN;
        return ans;
    }
};
时间: 2024-12-24 18:43:47

【LeetCode】008 String to Integer (atoi)的相关文章

【LeetCode】8. 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

【LeetCode】8 - 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 spe

LeetCode 008 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 b

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【LeetCode】Interleaving String 解题报告

[题目] Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. [解析] 题意:有

[LeetCode] NO. 8 String to Integer (atoi)

[题目] Implement atoi to convert a string to an integer. [题目解析] 该题目比较常见,从LeetCode上看代码通过率却只有13.7%,于是编码提交,反复修改了三四次才完全通过.该题目主要需要考虑各种测试用例的情况,比如"+5"."   67"."   +0078"."  12a56--".int的最大值最小值溢出等情况,通过这个题目,联系到实际项目中,我们一定要多考虑边界

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 this problem t

LeetCode Medium: 8. 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 b

LeetCode题解 #8 String to Integer (atoi)

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