We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d5129cb commit 224f3c8Copy full SHA for 224f3c8
solution/008. String to Integer (atoi)/Solution.js
@@ -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
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