Time:2019/4/19
Title: String To Integer
Difficulty: Medium
Author: 小鹿javascript
Implement atoi
which converts a string to an integer.java
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.git
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.github
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
函數,使其能將字符串轉換成整數。首先,該函數會根據須要丟棄無用的開頭空格字符,直到尋找到第一個非空格的字符爲止。數組
當咱們尋找到的第一個非空字符爲正或者負號時,則將該符號與以後面儘量多的連續數字組合起來,做爲該整數的正負號;假如第一個非空字符是數字,則直接將其與以後連續的數字字符組合起來,造成整數。ide
該字符串除了有效的整數部分以後也可能會存在多餘的字符,這些字符能夠被忽略,它們對於函數不該該形成影響。函數
注意:假如該字符串中的第一個非空格字符不是一個有效整數字符、字符串爲空或字符串僅包含空白字符時,則你的函數不須要進行轉換。測試
在任何狀況下,若函數不能進行有效的轉換時,請返回 0。
說明:
假設咱們的環境只能存儲 32 位大小的有符號整數,那麼其數值範圍爲 [−231, 231 − 1]。若是數值超過這個範圍,qing返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。
Note:
' '
is considered as whitespace character.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.
將字符串轉化爲數字,根據題目要求能夠分出一下幾種狀況:
- 若是字符串都是數字,直接進行轉換。
- 若是字符串中只有數字和空格,要跳過空格,只輸出數字。
- 數字前正負號要保留。
若是字符串中有數字和字母。
- 字母在數字前,直接返回數字 0 ;
- 字母在數字後,忽略數字後的字母;
- 若是輸出的數字大於限制值,輸出規定的限制值。
- 若是輸出的數字小於限制值,輸出規定的限制值。
上述將全部條件弄明白以後,而後進行規劃解決上述問題:
總體將字符串轉化爲每一個字符來進行判斷:1)空格:若是當前的字符爲空格,直接跳過,判斷下一字符。
2)符號位:若是判斷當前的字符爲 「 + 」 或 「 - 「,用 sign 存儲 1 或 -1,最後乘最後得出的數字。
3)只取數字:判斷當前是否知足條件(0<= c <=9),不知足條件直接跳出循環。
4)雖然取到數字,判斷數字是否超出最大值/最小值,咱們用判斷位數,最大值爲 7 位,每遍歷一個數組,咱們就進行乘以 10 加 個位數。
1)只輸入數字字符串2)帶空格、負數的字符串
3)帶字母的字符串
4)空字符串
5)超出限制的字符串
var myAtoi = function(str) { // 最大值與最小值 let MAX_VALUE = Math.pow(2,31)-1; let MIN_VALUE = -Math.pow(2,31); // 判斷字符窗是否爲空 if(str == null || str.length == 0) return 0; // 初始化 // sign:記錄正負號 // base: 記錄數字的位數 let [index,base,sign,len] = [0,0,1,str.length]; // 跳過空格 while(index < len && str.charAt(index) == ' '){ index++; } // 獲取符號位 if(index < len && (str.charAt(index) == "+") || str.charAt(index) == '-'){ // 記錄正負號 sign = 1 - 2 * ((str.charAt(index++) == '-') ? 1 : 0); } // 只取數字,碰到非數字退出循環 while(index < len && parseInt(str.charAt(index)) >= 0 && parseInt(str.charAt(index)) <= 9){ // 溢出判斷,MAX_VALUE 的個位爲 7 if(base > parseInt(MAX_VALUE/10) || (base == parseInt(MAX_VALUE/10) && parseInt(str.charAt(index)) > 7)){ if(sign == 1){ return MAX_VALUE; }else{ return MIN_VALUE; } } // 記錄位數 base = base * 10 + parseInt(str.charAt(index++)); } // 返回符號位 * 當前字符串中的數字 return sign * base; };
1)對字符串的基本操做。
2)代碼的全面性、魯棒性。