Skip to content

Commit 114bff6

Browse files
committed
Add Recursive Solution for Atoi Problem
1 parent 089efe6 commit 114bff6

File tree

2 files changed

+66
-26
lines changed

2 files changed

+66
-26
lines changed
Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
11
class Solution {
22
public:
3-
int myAtoi(string s) {
4-
5-
// INT_MAX : 2147483647
6-
// E.g. : 2147483648
7-
8-
int i = 0, n = s.size(), ans = 0, sign = 1;
9-
bool isSymbolUsed = false;
10-
11-
while (i<n && s[i]==' '){
12-
i++;
13-
}
14-
15-
while (i<n){
16-
char character = s[i];
17-
if (character == '-' || character == '+'){
18-
if (isSymbolUsed){
19-
break;
20-
} else if (character == '-'){
3+
int solve(string s, int ans, int index, int sign, bool isSymbolUsed){
4+
if (s[index]=='\0' || s[index]=='.' || s[index]==' ' || (isSymbolUsed && (s[index]=='+' || s[index]=='-'))){
5+
return ans;
6+
} else {
7+
if (s[index] == '-' || s[index] == '+'){
8+
if (s[index] == '-'){
219
sign = -1;
2210
}
2311
isSymbolUsed = true;
24-
} else if (character>='0' && character<='9'){
25-
int digit = character - '0';
26-
if ((ans > INT_MAX/10) || (ans==INT_MAX/10 && INT_MAX%10<=digit)){
12+
} else if (s[index]>='0' && s[index]<='9'){
13+
int digit = s[index]-'0';
14+
isSymbolUsed = true;
15+
if ((ans > INT_MAX/10) || (ans == INT_MAX/10 && digit%10>=INT_MAX%10)){
2716
return INT_MAX;
28-
} else if ((ans < INT_MIN/10) || (ans==INT_MIN/10 && (~INT_MIN%10+1)%10<=digit)){
17+
} else if ((ans < INT_MIN/10) || (ans == INT_MIN/10 && digit%10>=(~INT_MIN%10+1)%10)){
2918
return INT_MIN;
3019
} else {
3120
ans = ans*10 + sign*digit;
3221
}
33-
isSymbolUsed = true;
3422
} else {
35-
break;
23+
return ans;
3624
}
37-
i++;
25+
index++;
26+
return solve(s, ans, index, sign, isSymbolUsed);
27+
}
28+
}
29+
30+
int myAtoi(string s) {
31+
int ans = 0, sign = 1, index = 0;
32+
bool isSymbolUsed = false;
33+
34+
while (s[index]!='\0' && s[index]==' '){
35+
index++;
3836
}
3937

40-
return ans;
38+
return solve(s, ans, index, sign, isSymbolUsed);
4139
}
4240
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int myAtoi(string s) {
4+
5+
// INT_MAX : 2147483647
6+
// E.g. : 2147483648
7+
8+
int i = 0, n = s.size(), ans = 0, sign = 1;
9+
bool isSymbolUsed = false;
10+
11+
while (i<n && s[i]==' '){
12+
i++;
13+
}
14+
15+
while (i<n){
16+
char character = s[i];
17+
if (character == '-' || character == '+'){
18+
if (isSymbolUsed){
19+
break;
20+
} else if (character == '-'){
21+
sign = -1;
22+
}
23+
isSymbolUsed = true;
24+
} else if (character>='0' && character<='9'){
25+
int digit = character - '0';
26+
if ((ans > INT_MAX/10) || (ans==INT_MAX/10 && INT_MAX%10<=digit)){
27+
return INT_MAX;
28+
} else if ((ans < INT_MIN/10) || (ans==INT_MIN/10 && (~INT_MIN%10+1)%10<=digit)){
29+
return INT_MIN;
30+
} else {
31+
ans = ans*10 + sign*digit;
32+
}
33+
isSymbolUsed = true;
34+
} else {
35+
break;
36+
}
37+
i++;
38+
}
39+
40+
return ans;
41+
}
42+
};

0 commit comments

Comments
 (0)