forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString to Integer (atoi).js
46 lines (39 loc) · 1.2 KB
/
String to Integer (atoi).js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Runtime: 132 ms (Top 31.75%) | Memory: 45.5 MB (Top 21.64%)
function isNum(ch){
return !Number.isNaN(parseInt(ch));
}
var myAtoi = function(s) {
let index = 0;
// Remove whitespace
while(index < s.length && s[index] === ' '){
index++;
}
// Get the multiplier.
const MULTIPLIER = index < s.length && s[index] === '-' ? -1: 1;
// If the starting index is a sign char then move to the next character.
if(index < s.length && (s[index] === '-' || s[index] === '+')){
index++;
}
// Remove all leading zeros.
while(index < s.length && s[index] === '0'){
index++;
}
const MIN_NEG_INT = Math.pow(-2,31);
const MAX_POS_INT = (MIN_NEG_INT + 1) * -1;
// Get the first sequence number in the string.
let num = 0;
while(index < s.length && isNum(s[index])){
let digit = parseInt(s[index]);
if(num > Math.floor((MAX_POS_INT - digit)/10)){
// Handle overflow.
if(MULTIPLIER === 1){
return MAX_POS_INT;
}else{
return MIN_NEG_INT;
}
}
num = num * 10 + parseInt(s[index]);
index++;
}
return num * MULTIPLIER;
};