forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatoi.cpp
More file actions
43 lines (40 loc) · 1.05 KB
/
atoi.cpp
File metadata and controls
43 lines (40 loc) · 1.05 KB
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
39
40
41
42
43
#include <iostream>
#include <climits>
#include <string>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
class Solution {
public:
int atoi(const char *str) {
string s(str);
int len = s.length();
LL num = 0;
int idx = 0;
while (idx < len && s[idx] == ' ') ++idx;
int m = 1;
if (idx < len) {
if (s[idx] == '+') ++idx;
else if (s[idx] == '-') {
++idx;
m = -1;
}
}
for (; idx < len; ++idx) {
if (!(s[idx] >= '0' && s[idx] <= '9')) return num * m;
num = num * 10 + s[idx] - '0';
if (num * m > INT_MAX) return INT_MAX;
if (num * m < INT_MIN) return INT_MIN;
}
return num * m;
}
};
int main() {
Solution sol = Solution();
char *str = " 10000000000000000000000ad";
cout << sol.atoi(str) << endl;
return 0;
}