列表

详情


NC100. 把字符串转换成整数(atoi)

描述

写一个函数 StrToInt,实现把字符串转换成整数这个功能。不能使用 atoi 或者其他类似的库函数。传入的字符串可能有以下部分组成:
1.若干空格
2.(可选)一个符号字符('+' 或 '-')
3. 数字,字母,符号,空格组成的字符串表达式
4. 若干空格

转换算法如下:
1.去掉无用的前导空格
2.第一个非空字符为+或者-号时,作为该整数的正负号,如果没有符号,默认为正数
3.判断整数的有效部分:
3.1 确定符号位之后,与之后面尽可能多的连续数字组合起来成为有效整数数字,如果没有有效的整数部分,那么直接返回0
3.2 将字符串前面的整数部分取出,后面可能会存在存在多余的字符(字母,符号,空格等),这些字符可以被忽略,它们对于函数不应该造成影响
3.3  整数超过 32 位有符号整数范围 [−231,  231 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231的整数应该被调整为 −231 ,大于 231 − 1 的整数应该被调整为 231 − 1
4.去掉无用的后导空格


数据范围:
1.0 <=字符串长度<= 100
2.字符串由英文字母(大写和小写)、数字(0-9)、' '、'+'、'-' 和 '.' 组成


示例1

输入:

"82"

输出:

82

示例2

输入:

"   -12  "

输出:

-12

说明:

去掉前后的空格,为-12

示例3

输入:

"4396 clearlove"

输出:

4396

说明:

6后面的字符不属于有效的整数部分,去除,但是返回前面提取的有效部分

示例4

输入:

"clearlove 4396"

输出:

0

示例5

输入:

"-987654321111"

输出:

-2147483648

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 432KB, 提交时间: 2022-02-09

class Solution {
public:
    int StrToInt(string s) {
        int n = s.length();
        // 去除前导空格
        int index = 0; //遍历字符串的下标
        while(index < n) {
            if (s[index] != ' ')
                break;
            index++;
        }
        if(index == n) //除了0就没有了
            return 0;
        int sign = 1;
        // 处理第 1 个非空字符为正负符号
        if(s[index] == '+')
            index++;
        else if(s[index] == '-') {
            sign = -1;
            index++;
        }
        int res = 0;
        while(index < n){
            char c = s[index];
            if(c < '0' || c > '9') //非数字跳出
                break;
            //int型最大最小
            if(res > INT_MAX / 10 || (res == INT_MAX / 10 && (c - '0') > INT_MAX % 10))
                return INT_MAX;
            if(res < INT_MIN / 10 || (res == INT_MIN / 10 && (c - '0') < (-INT_MIN) % 10))
                return INT_MIN;
            res = res * 10 + sign * (c - '0');
            index++;
        }
        return res;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 480KB, 提交时间: 2022-02-09

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return int整型
     */
    int StrToInt(string s) {
        // write code here
        int n = s.length();
        int index = 0;
        while(index < n)
        {
            if(s[index]!= ' ')
                break;
            index ++;
        }
        bool flag = 1;
        if(is_op(s[index],flag))
            index ++;
        
        long num = 0;
        while(index < n )
        {
            if(!is_num(s[index]) )
                break;
            else
                num = num *10 + s[index]- '0';
            index ++;
            if(-num < INT_MIN)
                break;
        }
        if(!flag)
            num = -1 * num;
        num = num>INT_MAX? INT_MAX:(num< INT_MIN? INT_MIN:num);
        return num;
        
        
    }
    
    bool is_op( char c, bool & flag)
    {
        if(c == '+')
            return true;
        if(c == '-'){
            flag = false;
            return true;
        }
        else return false;
    }
    
    bool is_num(char c)
    {
        if(c<='9' && c >='0')
            return true;
        else return false;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 512KB, 提交时间: 2022-02-09

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return int整型
     */
    
    bool avail(char ch) {
        if (ch == ' ') {
            return true;
        }
        if (ch == '-' || ch == '+') {
            return true;
        }
        if (ch >='0' && ch <= '9') {
            return true;
        }
        return false;
    }
    int StrToInt(string s) {
        bool foundSign = false;
        bool sign = true;
        bool isBegin = false;
        string strNum;

        for (const auto& ch : s) {
            if (!avail(ch)) {
                break;
            }
            if (isBegin && (ch > '9' || ch < '0')) {
                // 已经开始,但是遇到非数字
                break;
            }
            if (ch == '-' || ch == '+') {
                isBegin = true;
                if (ch == '-') {
                    sign = false;
                }
            }
            if (ch <= '9' && ch >= '0') {
                strNum += ch;
                isBegin = true;
            }
        }
        long res = 0;
        long maxOne = (long)INT32_MAX+1;
        for (const auto& ch : strNum) {
            res *= 10;
            res += ch - '0';
            if (res >= maxOne) {
                res = maxOne;
                break;
            }
        }
        if (res == maxOne) {
            if (!sign) {
                return INT32_MIN;
            }
            return INT32_MAX;
        }
        if (sign) {
            return res;
        } else {
            return -res;
        }
    }
};

C++ 解法, 执行用时: 3ms, 内存消耗: 408KB, 提交时间: 2022-07-25

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return int整型
     */
    int StrToInt(string s) {
        // write code here
        int res = 0;
        int index = 0;
        int n = s.length();
        while(index < n)
        {
            if(s[index] == ' ')
                index++;
            else
                break;
        }
        if(index == n)
            return 0;
        int sign = 1;
        if(s[index] == '+')
            index++;
        else if(s[index] == '-')
        {
            index++;
            sign = -1;
        }
        if(index == n)
            return 0;
        while(index < n)
        {
            char c = s[index];
            if(c < '0' || c > '9')
                break;
            if(res > INT_MAX / 10 || (res == INT_MAX / 10 && (c - '0') > INT_MAX % 10))
                return INT_MAX;
            if(res < INT_MIN / 10 || (res == INT_MIN / 10 && (c - '0') > -(INT_MIN % 10)))
                return INT_MIN;
            res = res * 10 + sign * (c - '0');
            index++;
        }
        return res;
    }
};

C++ 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2021-12-07

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return int整型
     */
    int StrToInt(string s) {
        int n = s.length();
        // 去除前导空格
        int index = 0; //遍历字符串的下标
        while(index < n) {
            if (s[index] != ' ')
                break;
            index++;
        }
        if(index == n) //除了0就没有了
            return 0;
        int sign = 1;
        // 处理第 1 个非空字符为正负符号
        if(s[index] == '+')
            index++;
        else if(s[index] == '-') {
            sign = -1;
            index++;
        }
        int res = 0;
        while(index < n){
            char c = s[index];
            if(c < '0' || c > '9') //非数字跳出
                break;
            //int型最大最小
            if(res > INT_MAX / 10 || (res == INT_MAX / 10 && (c - '0') > INT_MAX % 10))
                return INT_MAX;
            if(res < INT_MIN / 10 || (res == INT_MIN / 10 && (c- '0') > -(INT_MIN % 10)))
                return INT_MIN;
            res = res * 10 + sign * (c - '0');
            index++;
        }
        return res;
    }
};

上一题