8. String to Integer (atoi)

problem

solution

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
class Solution {
public:
int myAtoi(string s) {
int i = 0;
while(s[i] == ' ') i++;
int flag = 1;
if(s[i] == '-' || s[i] == '+'){

if(s[i] =='-') flag = -1;
i++;
}
int ret = 0;
for(int j = i;j<s.size() ; ++j){
if(s[j]-'0' >=0 && s[j]-'0'<=9){
if(ret>INT_MAX/10 || (ret==INT_MAX/10 && s[j]-'0'>INT_MAX%10)){
return flag==1?INT_MAX:INT_MIN;
}
ret*= 10;
ret += s[j]-'0';
}
else break;
}
return ret*flag;
}
};

analysis

  • time complexity O(n)
  • space compleity O(n)