-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.cpp
More file actions
46 lines (40 loc) · 981 Bytes
/
Copy path8.cpp
File metadata and controls
46 lines (40 loc) · 981 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
39
40
41
42
43
44
45
46
/**
* @file 8.cpp
* @author your name ([email protected])
* @brief
* @version 0.1
* @date 2022-06-12
*
* @copyright Copyright (c) 2022
*
*/
class Solution {
public:
const int INF = 2147483647;
const int INF2 = 2147483648;
int myAtoi(string s) {
if (s.empty()) return 0;
int i = 0;
// 去掉前导空格
while (i < s.size() && s[i] == ' ') i++;
// 处理负号
int sign = 1;
if (s[i] == '-') {
sign = -1;
i++;
} else if (s[i] == '+') {
sign = 1;
i++;
}
// 处理数字部分
int ans = 0;
while (i < s.size() && isdigit(s[i])) {
int d = s[i] - '0';
if (ans > INF / 10) return sign == -1 ? INF2 : INF;
if (ans == INF / 10 && d > 7) return sign == -1 ? INF2 : INF;
ans = ans * 10 + d;
i++;
}
return sign == -1 ? -1 * ans : ans;
}
};