Skip to content

Commit 224f3c8

Browse files
add solution 008[js]
1 parent d5129cb commit 224f3c8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const myAtoi = function(str){
2+
str = str.trim();
3+
if(!str) return 0;
4+
let isPositive = 1;
5+
let i = 0, ans = 0;
6+
if(str[i] === '+'){
7+
isPositive = 1;
8+
i++;
9+
}else if(str[i] === '-'){
10+
isPositive = 0;
11+
i++;
12+
}
13+
for(; i < str.length; i++){
14+
let t = str.charCodeAt(i) - 48;
15+
if(t > 9 || t < 0) break;
16+
if(ans > 2147483647/10 || ans > (2147483647-t)/10){
17+
return isPositive ? 2147483647 : -2147483648;
18+
}else{
19+
ans = ans*10 + t;
20+
}
21+
}
22+
return isPositive? ans : -ans;
23+
}

0 commit comments

Comments
 (0)