-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_LC8.cpp
More file actions
69 lines (66 loc) · 1.51 KB
/
string_LC8.cpp
File metadata and controls
69 lines (66 loc) · 1.51 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;
#include<string>
//#include<stack>
#include<set>
#define INT_MAX 2147483647
#define INT_MIN -2147483647-1
class Solution {
public:
int myAtoi(string str) {
//stack<char> reversed;
char allowed_char[] = { '0', '1','2','3','4','5','6','7','8','9','+','-' };
set<char> allowed(allowed_char, allowed_char + 12);
set<char> allowed_num(allowed_char, allowed_char + 10);
//str 为空时
if (str == "")return 0;
//str 不为空
int length = str.size();
long long res = 0;
int i = 0;
//去掉前面的空白字符
while (i<length&&str[i]==' ') {
i++;
}
//全为空白字符,或者第一个非空字符不合法
if (length == i||allowed.find(str[i])==allowed.end()) {
return 0;
}
//第一个非空字符的情况
if (str[i] == '+' || str[i] == '-') {
//int sign = 1;
//if (str[i] == '-')sign = -1;
char temp = str[i];
i++;
if (allowed_num.find(str[i]) == allowed_num.end()) {
return 0;
}
while (i<length&&allowed_num.find(str[i]) != allowed_num.end()) {
res = res*10 + str[i] - '0';
if (res > INT_MAX ) {
if (temp == '+') {
return INT_MAX;
}
else {
return INT_MIN;
}
}
i++;
}
if (temp == '+') {
return (int)res;
}
else return - (int)res;
}
else {
while (i<length&&allowed_num.find(str[i]) != allowed_num.end()) {
res = res * 10 + str[i] - '0';
if (res >= INT_MAX) {
return INT_MAX;
}
i++;
}
return (int)res;
}
}
};