forked from jyxia/LeetCode-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8-stringToInteger.js
36 lines (32 loc) · 900 Bytes
/
8-stringToInteger.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
/**
* @param {string} str
* @return {number}
*/
var myAtoi = function(str) {
if (str.length === 0) return 0;
var index = 0;
var sign = 1;
var num = 0;
// delete spaces before and after, trim() is a good method.
str = str.trim();
// not clear from the question, the string may not begin with a sign.
if (str[index] === '-' ) {
sign = -1;
index++;
} else if (str[index] === '+' ) {
index++;
}
for (var i = index; i < str.length; i++) {
if (str[i] < '0' || str[i] > '9') break;
num = num * 10 + parseInt(str[i]);
}
// ugly here, don't how to represent the max number in JavaScript.
var MAXVAULE = Math.pow(2, 31) - 1;
if (sign === 1 && num >= MAXVAULE) {
return MAXVAULE;
}
if (sign === -1 && -num <= -(MAXVAULE+1)) {
return -(MAXVAULE+1);
}
return num * sign;
};