String to Integer (atoi)

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求

Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:
Only the space character ’ ’ is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:
Input: "42"
Output:42

Example 2:
Input:" -42"
Output:-42
Explanation: The first non-whitespace character is ‘-’, which is the minus sign.
Then take as many numerical digits as possible, which gets 42.

Example 3:
Input:"4193 with words"
Output:4193
Explanation: Conversion stops at digit ‘3’ as the next character is not a numerical digit.

Example 4:
Input:"words and 987"
Output:0
Explanation: The first non-whitespace character is ‘w’, which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:
Input: "-91283472332"
Output:-2147483648
Explanation: The number “-91283472332” is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.

实现** atoi **,将字符串转换为整数。

该函数首先丢弃所需数量的空白字符,直到找到第一个非空白字符。然后,从该字符开始,采用可选的初始加号或减号,后跟尽可能多的数字,并将它们解释为数值。

字符串可以包含在形成整数之后的其他字符,这些字符将被忽略并且对此函数的行为没有影响。

如果str中的第一个非空白字符序列不是有效的整数,或者由于str是空的或者只包含空白字符而不存在这样的序列,则不执行转换。

如果无法执行有效转换,则返回零值。

注意:
只有空格字符’'被视为空格字符。
假设我们正在处理一个只能在32位有符号整数范围内存储整数的环境:[ - 231,231 - 1]。如果数值超出可表示值的范围,则返回INT_MAX(231-1)或INT_MIN(-231)。

2,题目思路

对于这道题,要求实现atoi——即将字符串转换为数字。

字符串数字转换为数字本身并不难,关键在于不是数字以及其他字符的判断。

在这道题中,首先,字符的前面是有可能存在大量的空格的,这里,我们使用string类对象的成员函数,用来寻找第一个非" "的元素的下标。
这里需要注意的是,给出的字符串是有可能全部都是空格的,这时返回值为-1,因此,我们需要对这种情况做单独的判断。

其次,就是符号判断,这点比较容易。

最后,就是需要时刻对越界与否进行判断,即res*sign需要在INT_MIN和INT_MAX之间,为了实现和这两个边界值进行判断,res需要定义为long long。

3,代码实现

static auto speedup = [](){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    int myAtoi(string str) {
        long long res = 0;
        int sign = 1;
        
        //去掉前面所有的空格
        int i = str.find_first_not_of(' ');
        if(i == -1)
            return 0;
        //判断数字的符号
        if(str[i] == '-' || str[i] == '+')
            sign = (str[i++] == '-')? -1 : 1;
        
        while(str[i]>='0' && str[i]<='9'){
            res = res*10 + (str[i++]-'0');
            //越界判断
            if(res*sign >= INT_MAX) return INT_MAX;
            if(res*sign <= INT_MIN) return INT_MIN;
        }
        return sign*res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值