LeetCode 8: String to Integer (atoi)

Description:

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函数,将字符串转换为整数。

首先,该函数丢弃字符串起始的空白字符,直到第一个非空白字符。然后,由该非空白字符开始,为可选的正号或负号,其后是数字。

数字后面可以包含其他非数字字符,这不影响函数的解析过程。

如果首个非空格字符不是有效的整数负号(即,不是数字和正负号),或者字符串为空的或者仅包含空白字符,那么不需要转换操作。这时,函数返回0。

注意:

  1. 只有空格' '被认为空白字符。

  2. 假设转换后的数字为32位有符号数,区间为[-232, 232-1]。如果相应的数字超出范围,则返回-232或者232-1。

例子1:

输入:“42”
输出:42

例子2:

输入:“    -42”
输出:-42

例子3:

输入: "4193 with words"
输出: 4193

例子4:

输入:"words and 987"
输出:0
解释:首个非空白字符不是有效的数字符号或者数字

例子5:

输入:"-91283472332"
输出:-2147483648
解释:数字超出表示范围。
class Solution {
public:
    int myAtoi(string str) {
        if(str.length() == 0)
            return 0;
        int i = 0;
        int ret = 0;
        while(str[i] == ' ') {
            i++;
        }
        int sign = 0;
        if(str[i] == '+') {
            sign = 0;
            i++;
        } else if(str[i] == '-') {
            sign = 1;
            i++;
        }
        while(i < str.length()) {
            if(str[i] < '0' || str[i] > '9')
                break;
            int n = str[i] - '0';
            if(sign) {
                n = n * -1;
            }
            if(ret > INT_MAX / 10 || (ret == INT_MAX / 10 && n > 7))
                return INT_MAX;
            if(ret < INT_MIN / 10 || (ret == INT_MIN / 10 && n < -8))
                return INT_MIN;
            ret = ret * 10 + n;
            i++;
        }
        return ret;
    }
};

  时间复杂度为O(n),空间复杂度为O(1)。

猜你喜欢

转载自www.cnblogs.com/alpaca/p/9488451.html