LintCode-54.转换字符串到整数

转换字符串到整数

实现atoi这个函数,将一个字符串转换为整数。如果没有合法的整数,返回0。如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-2147483648)如果是负整数。

样例

"10" =>10
"-1" => -1
"123123123123123" => 2147483647
"1.0" => 1

标签

字符串处理 基本实现 优步

code

class Solution {
public:
    /**
     * @param str: A string
     * @return An integer
     */
    int atoi(string str) {
        // write your code here
        str = eraseSpace(str);
        int size=str.size();

        if(size <= 0)
            return 0;

        if(str[0]<='9' && str[0]>='0') {
            int num=str[0]-'0', i, dec=str[0]-'0';
            for(i=1; i<size; i++) {
                if(str[i]<='9' && str[i]>='0') {
                    dec = num;
                    num = num * 10;
                    if(isCrossBorder(num, dec))
                        return 2147483647;

                    dec = num;
                    num += str[i]-'0';
                    if(isCrossBorder(num, dec))
                        return 2147483647;
                }
                else {
                    return num;
                }
            }
            return num;
        }
        else if(str[0] == '+') {
            int num=0, i, dec=0;
            for(i=1; i<size; i++) {
                if(str[i]<='9' && str[i]>='0') {
                    dec = num;
                    num = num * 10;
                    if(isCrossBorder(num, dec))
                        return 2147483647;

                    dec = num;
                    num += str[i]-'0';
                    if(isCrossBorder(num, dec))
                        return 2147483647;
                }
                else {
                    return num;
                }
            }
            return num;
        }
        else if(str[0] == '-') {
            int num=0, i, dec=0;
            for(i=1; i<size; i++) {
                if(str[i]<='9' && str[i]>='0')  {
                    dec = num;
                    num = num * 10;
                    if(isCrossBorder(num, dec))
                        return -2147483648;

                    dec = num;
                    num += str[i]-'0';
                    if(isCrossBorder(num, dec))
                        return -2147483648;
                }
                else {
                    return num*-1;
                }
            }
            return num*-1;
        }
        else {
            return 0;
        }
    }

    bool isCrossBorder(int num1, int num2) {
        if(num1 < num2)
            return true;
        return false;
    }

    string eraseSpace(string str) {
        int size=str.size(),i=0,mark=0;
        string result;

        for(i=0; i<size; i++) {
            if(str[i]==' ' && mark==0) {
                continue;
            }
            else if(str[i]!=' ' && mark==0)  {
                mark = 1;
                result += str[i];
            }
            else if(str[i]!=' ' && mark==1) {
                mark = 1;
                result += str[i];
            }
            else if(str[i]==' ' && mark==1) {
                break;
            }
        }
        return result;
    }
};
posted @ 2017-05-04 17:11  LiBaoquan  阅读(278)  评论(0编辑  收藏  举报