1
/*
2

3
Time Complexity : O(logN), Since we are going through the entire number
4
digit by digit, the time complexity should be O(log10N). The reason behind
5
log10 is because we are dealing with integers which are base 10.
6

7
Space Complexity : O(1), We are not using any data structure for interim
8
operations, therefore, the space complexity is O(1).
9

10
Solved using String.
11

12
*/
13

14
class Solution {
15
public:
16
int myAtoi(string s) {
17
int len = s.size();
18
double num = 0;
19
int i = 0;
20
while (s[i] == ' ') {
21
i++;
22
}
23
bool positive = s[i] == '+';
24
bool negative = s[i] == '-';
25
positive == true ? i++ : i;
26
negative == true ? i++ : i;
27
while (i < len && s[i] >= '0' && s[i] <= '9') {
28
num = num * 10 + (s[i] - '0');
29
i++;
30
}
31
num = negative ? -num : num;
32
cout << num << endl;
33
num = (num > INT_MAX) ? INT_MAX : num;
34
num = (num < INT_MIN) ? INT_MIN : num;
35
cout << num << endl;
36
return int(num);
37
}
38
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0