-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.cpp
More file actions
executable file
·38 lines (34 loc) · 909 Bytes
/
8.cpp
File metadata and controls
executable file
·38 lines (34 loc) · 909 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdlib>
#include <string>
#include <climits>
#include <cctype>
using namespace std;
class Solution {
public:
int myAtoi(string str)
{
int result = 0;
int sign = 1;
int i = str.find_first_not_of(" ");
if (str[i] == '+' || str[i] == '-')
sign = (str[i++] == '-')? -1 : 1;//don't understand
while (isdigit(str[i]) && i < str.size())
{
result = result * 10 + (str[i] - '0');
cout << "res = " << result << endl;
cout << "LONG_MAX: " << LONG_MAX << endl;
if (result > LONG_MAX && sign == '1') return LONG_MAX;
if (result < LONG_MIN && sign == '-1') return LONG_MIN;
i++;
}
return result*sign;
}
};
int main()
{
Solution S;
string x = "2147483648";
int result = S.myAtoi(x);
cout << "Result = " << result << endl;
}