-
Notifications
You must be signed in to change notification settings - Fork 0
/
191005-1.cpp
46 lines (44 loc) · 1.13 KB
/
191005-1.cpp
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
44
45
46
// https://leetcode-cn.com/problems/string-to-integer-atoi/
#include <cstdio>
#include <string>
using namespace std;
class Solution {
public:
const int MAX_VAL = 0x7FFFFFFF / 10;
const int MAX_DIGIT = 0x7FFFFFFF % 10;
int myAtoi(string str) {
bool negative = false;
unsigned int r = 0;
const char* p = str.c_str();
while (*p == ' ' || *p == '\t') {
++p;
}
if (*p == '-') {
negative = true;
++p;
} else if (*p == '+') {
++p;
}
if (*p < '0' || *p > '9') {
return 0;
}
for (; *p >= '0' && *p <= '9'; ++p) {
int d = *p - '0';
if ((r > MAX_VAL) || ((r == MAX_VAL) && (d > MAX_DIGIT + negative))) {
return (negative ? -0x80000000 : 0x7FFFFFFF);
}
r = 10 * r + d;
}
return (int)(negative ? ~(r - 1) : r);
}
};
int main() {
Solution s;
printf("%d\n", s.myAtoi("42")); // answer: 42
printf("%d\n", s.myAtoi(" -42")); // answer: -42
printf("%d\n", s.myAtoi("4193 with words")); // answer: 4193
printf("%d\n", s.myAtoi("words and 987")); // answer: 987
printf("%d\n", s.myAtoi("-91283472332")); // answer: -2147483648
printf("%d\n", s.myAtoi("-2147483648")); // answer: -2147483648
return 0;
}