-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToInteger.js
More file actions
50 lines (43 loc) · 1.19 KB
/
Copy pathStringToInteger.js
File metadata and controls
50 lines (43 loc) · 1.19 KB
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
47
48
49
50
"use strict"
/**
* @param {string} s
* @return {number}
*/
/* Result
Runtime: 85 ms, faster than 75.47% of JavaScript online submissions for String to Integer (atoi).
Memory Usage: 45.3 MB, less than 31.47% of JavaScript online submissions for String to Integer (atoi).
*/
var myAtoi = function (s) {
let result = 0;
let index = 0;
let sign = 1;
//trim off leading whitespace
while (s[index] === " ") {
index++;
}
if (s[index] + s[index + 1] === "+-") return 0;
if (s[index] === "+") index++;
//check for negative sign
if (s[index] == "-") {
sign = -1;
index++;
}
while (s.charCodeAt(index) >= 48 && s.charCodeAt(index) <= 57) {
result = result * 10 + (s.charCodeAt(index) - 48);
index++;
}
result = result * sign;
if (result < -2147483648)
return -2147483648
else if (result > 2147483647)
return 2147483647;
else
return result;
};
(function test() {
console.log("0 " + myAtoi("+-12"));
console.log("42 " + myAtoi("+42"));
console.log("42 " + myAtoi("42"));
console.log("-42 " + myAtoi(" -42"));
console.log("4193 " + myAtoi("4193 with words"));
})()