LeetCode Js-8. String to Integer

    LeetCode Js-String

    Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++’s atoi function).

    Constraints:

    1.0 <= s.length <= 200
    2.s consists of English letters (lower-case and upper-case),
       digits (0-9), ' ', '+', '-', and '.'.
    

    Note:
    Only the space character ' ' is considered a whitespace character.
    Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.

    Example 1:

    Input: s = "42"
    Output: 42
    

    Example 2:

    Input: s = "   -42"
    Output: -42
    

    Example 3:

    Input: s = "4193 with words"
    Output: 4193
    

    solution:
    1.確認數值的正負狀態
    如果是「+」,則default = 1
    如果是「-」,則default = -1
    2.確認數值是否為整數
    移除前後空白
    3.確認數值後是否有文字
    移除不考慮
    4.數值的處理
    詳請月FlowChart

    Code:

    var myAtoi = function(s) {
       // "   -321321 bunnies" 
       //ignore leading whitespaces
        let index = 0;
        while (s[index] === " ") {
            index++
        }
        //check if the next character is '-' or '+'
        let sign = 1 //default to '+'
        if (s[index] === '+') {
            sign = 1
            index++
        } else if (s[index] === "-") {
            sign = -1
            index++
        }
        //read string of digits into integer
        const digits = {}
        for (let i = 0; i < 10; i++) {
            digits[i + ""] = i
        }
        let result = 0
        for (let i = index; i < s.length; i++) {
            if (digits[s[i]] !== undefined) { // is digit
                result = result * 10 + digits[s[i]] //重點
            } else {
                break
            }
        }
        // clamped to max or min
        result = result * sign
        if (result < -1 * Math.pow(2, 31)) {
            result = -1 * Math.pow(2, 31)
        } else if (result > Math.pow(2, 31) -1) {
            result = Math.pow(2, 31) -1
        }
        return result
    };
    

    FlowChart:
    Example 1

    result = 0
    result = result * 10 + digits[s[i]] => 0 + 4 = 4
    result = result * 10 + digits[s[i]] => 4 * 10 + 2 = 42
    
    Output = 42
    

    Example 2:

    (s[count] === " ") => count = 1
    (s[count] === " ") => count = 2
    (s[count] === " ") => count = 3
    (s[count] === " ") => count = 4
    (s[count]) === "-") => default = -1;count = 5
    
    result = 0
    result = result * 10 + digits[s[i]] => 0 + 4 = 4
    result = result * 10 + digits[s[i]] => 4 * 10 + 2 = 42
    
    default * result = -1 * 42 => -42
    
    Output = -42
    

    Example 3:

    
    result = 0
    result = result * 10 + digits[s[i]] => 0 + 4 = 4
    result = result * 10 + digits[s[i]] => 4 * 10 + 1 = 41
    result = result * 10 + digits[s[i]] => 41 * 10 + 9 = 419
    result = result * 10 + digits[s[i]] => 419 * 10 + 3 = 4193
    
    (digits[s[i]] === underfined) => break
    
    Output = 4193
    

    Learning URL