LeetCode Js-8. String to Integer (atoi)

    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).

    實現 myAtoi(string s)函式,每一個 a 字串的轉換為 32 位元符號整數。
    
    - 忽略字串前方的前導空格。
    - 檢查字串前的「-」與「+」符號,並給予正確的渲染。
    - 當下一個非數字字串時,將其餘部分忽略。
    - 如傳入空值時,則回覆 0。
    - 回傳介於 -2^31 <= a <= 2^31。
    
    ex. Atoi = ASCII to integer
    

    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 1

    var myAtoi = function(s: string): number {
      // "   -321321 bunnies" 
      //ignore leading whitespaces
      let index: number = 0;
      while (s[index] === " ") {
        index++
      }
      //check if the next character is '-' or '+'
      let sign: number = 1 //default to '+'
      if (s[index] === '+') {
        sign = 1
        index++
      } else if (s[index] === "-") {
        sign = -1
        index++
      }
      //read string of digits into integer
      const digits: Record = {}
      for (let i = 0; i < 10; i++) {
        digits[i + ""] = i
      }
      let result: number = 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
    

    Success
    Details
    Runtime: 131 ms, faster than 22.67% of JavaScript online submissions for String to Integer (atoi).
    Memory Usage: 45.3 MB, less than 31.49% of JavaScript online submissions for String to Integer (atoi).

    Learning URL

    Code 2

    var myAtoi = function(s: string): number {
        const res: number = parseInt(s) || 0;
    
        if (res > 2**31 - 1) return 2**31 - 1;
        if (res < 2**31 * - 1) return 2**31 * -1;
        return res;
    };