Published Oct 20, 2021
[
 
]
Implement the myAtoi(string s)
function, which converts a string to a 32-bit signed integer (similar to C/C++’s atoi
function).
The algorithm for myAtoi(string s)
is as follows:
' '
is considered a whitespace character.Input: s = "42"
Output: 42
Explanation: The underlined characters are what is read in, the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
^
Step 3: "42" ("42" is read in)
^
The parsed integer is 42.
Since 42 is in the range [-231, 231 - 1], the final result is 42.
Input: s = " -42"
Output: -42
Explanation:
Step 1: " -42" (leading whitespace is read and ignored)
^
Step 2: " -42" ('-' is read, so the result should be negative)
^
Step 3: " -42" ("42" is read in)
^
The parsed integer is -42.
Since -42 is in the range [-231, 231 - 1], the final result is -42.
Input: s = "4193 with words"
Output: 4193
Explanation:
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
^
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
^
Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit)
^
The parsed integer is 4193.
Since 4193 is in the range [-231, 231 - 1], the final result is 4193.
Input: s = "words and 987"
Output: 0
Explanation:
Step 1: "words and 987" (no characters read because there is no leading whitespace)
^
Step 2: "words and 987" (no characters read because there is neither a '-' nor '+')
^
Step 3: "words and 987" (reading stops immediately because there is a non-digit 'w')
^
The parsed integer is 0 because no digits were read.
Since 0 is in the range [-231, 231 - 1], the final result is 0.
Input: s = "-91283472332"
Output: -2147483648
Explanation:
Step 1: "-91283472332" (no characters read because there is no leading whitespace)
^
Step 2: "-91283472332" ('-' is read, so the result should be negative)
^
Step 3: "-91283472332" ("91283472332" is read in)
^
The parsed integer is -91283472332.
Since -91283472332 is less than the lower bound of the range [-231, 231 - 1], the final result is clamped to -231 = -2147483648.
0 <= s.length <= 200
s
consists of English letters (lower-case and upper-case), digits (0-9)
,
' '
, '+'
, '-'
, and '.'
.function myAtoi(s: string): number {
if(!s) return 0;
let ipStr = s.trim(); // trim all leading space
let startIdx = 0; // initially point to index 0
let intVal = "";
let sign = "";
let lowerInt = -0x80000000; // min integer
let upperInt = 0x7FFFFFFF; // max integer
while(startIdx < ipStr.length){
if((ipStr[startIdx] === '-' || ipStr[startIdx] === '+') && !sign && !intVal){ // store sign only when it first sign
sign = ipStr[startIdx];
} else if(ipStr[startIdx] >= '0' && ipStr[startIdx] <= '9'){
intVal += ipStr[startIdx] //store only digit character
}else break; // found non-digit break the loop
startIdx++;
}
if(intVal === '') return 0;
return !sign || sign === '+' ? Math.min(+parseInt(intVal),upperInt) : Math.max(-parseInt(intVal),lowerInt);
};