LeetCode 8 — String to Integer (atoi)(字符串轉整數 (atoi))

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。
說明:
假設我們的環境只能存儲 32 位有符號整數,其數值範圍是 [−231, 231 − 1]。如果數值超過可表示的範圍,則返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。

示例 1:
輸入: “42”
輸出: 42
示例 2:
輸入: " -42"
輸出: -42
解釋: 第一個非空白字符爲 ‘-’, 它是一個負號。
我們儘可能將負號與後面所有連續出現的數字組合起來,最後得到 -42 。
示例 3:
輸入: “4193 with words”
輸出: 4193
解釋: 轉換截止於數字 ‘3’ ,因爲它的下一個字符不爲數字。
示例 4:
輸入: “words and 987”
輸出: 0
解釋: 第一個非空字符是 ‘w’, 但它不是數字或正、負號。
因此無法執行有效的轉換。
示例 5:
輸入: “-91283472332”
輸出: -2147483648
解釋: 數字 “-91283472332” 超過 32 位有符號整數範圍。
因此返回 INT_MIN (−231) 。

分析
簡單的字符串處理,但是細節很多,一次就編譯通過說明你很牛逼。

c++實現

class Solution {
public:
    int myAtoi(string str) {
        int first = 0;
        bool fushu = false;
        string shuzhi = "";
        while (str[first] == ' ')
            first++;
        if (str[first] == '-')
        {
            first++;
            fushu = true;
        }
        if (str[first] == '+' && !fushu)
            first++;
        if (str[first] > 57 || str[first] < 48)
            return 0;
        for (int i = first; i < str.length(); i++)
        {
            if (str[i] <= 57 && str[i] >= 48)
                shuzhi += str[i];
            else
                break;
        }
        int start = 0;
        while (shuzhi[start] == '0')
            start++;
        int length = shuzhi.length() - start;
        if (length > 10 && fushu)
            return -2147483648;
        if (length > 10 && !fushu)
            return 2147483647;
        long tmp = stol(shuzhi);
        if (tmp >= 2147483647 && !fushu)
            return 2147483647;
        if (tmp >= 2147483648 && fushu)
            return -2147483648;
        int res = stoi(shuzhi);
        if (fushu)
            return -res;
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章