1. 程式人生 > >String to Integer (atoi)——LeetCode進階路⑧

String to Integer (atoi)——LeetCode進階路⑧

原題連結https://leetcode.com/problems/string-to-integer-atoi/

說實話,看到這道題之前,看這通過率有點慌,到底是因為啥 讓一道medium的題目這麼“厲害”咧,接著往下瞧~

  • problem 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.

    實現將字串轉換為整數的atoi。

    函式首先丟棄儘可能多的空格字元,直到找到第一個非空格字元。然後,從這個字元開始,取一個可選的初始加號或減號,後面跟著儘可能多的數字,並將它們解釋為一個數值。

    字串可以在組成整數的字元之後包含其他字元,這些字元將被忽略,並且對該函式的行為沒有影響。

    如果str中的非空格字元的第一個序列不是有效的整數,或者由於str為空或只包含空格字元而不存在這樣的序列,則不執行轉換。

    如果不能執行有效的轉換,則返回零值。

    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.

    注意:

    只有空格字元' '被認為是空格字元。

    假設我們正在處理一個環境,只能在32位帶符號整數儲存整數範圍:(231−231−1)。如果數值的範圍可表示的值,INT_MAX(231−1)或INT_MIN返回(231−)。

  • 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.
  • thinking:讀罷題目,應該會明確,之所以準確率不高應該與情況較多有關係。
    1.刪掉空格
    2.判斷是否有正負號,但是隻能有一個,否則判為無效轉換返回0
    3.一旦遇到非數字,停止轉換
    4.出現數據溢位時,返回邊界值
  • 原始碼附錄:
    class Solution {
        public int myAtoi(String str) {        
            str = str.trim();//刪空格
            
            if(str == null || str.length() < 1){
                return 0;
            }
            
            int flag = 1;
            int index = 0;
            long result = 0;
            
            if(str.charAt(index) == '-'){
                flag = -1;
                index ++;
            }
            else if(str.charAt(index) == '+'){
                index ++;
            }
            
            for(;index<str.length();index ++){
                int t = str.charAt(index) - '0';
                if(t>9 || t<0){
                   break;
                }
                
                if(flag == 1){
                     result = result*10 + t;                
                    if(result > Integer.MAX_VALUE){
                         return Integer.MAX_VALUE;
                    }
                }
                else{
                    result = result*10 - t;
                    if(result < Integer.MIN_VALUE){
                    return Integer.MIN_VALUE;
                }
             }
            }  
            
            int temp = (int)result;
            return temp;
        }
    }

    #四級over 寒假之前被實訓和複習安排的明明白白,LeetCode……寒假見