-
Notifications
You must be signed in to change notification settings - Fork 0
/
08-String_to_Integer.cpp
106 lines (99 loc) · 2.86 KB
/
08-String_to_Integer.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
*/
#include <iostream>
#include <climits>
using namespace std;
class Solution {
public:
int myAtoi(string str) {
int res = 0;
bool noFlags = true;
if(str.length() == 0) return 0; //空字符串
if(str[0] < '0' && str[0] > '9' && str[0] != '-') return 0; //第一个字符不是数字
while(str[0] == ' ' || str[0] == '0' || str[0] == '+') { //去掉无用字符
if(str[0] == '+') {
noFlags = false;
}
if(str[0] == '+' && str[1] == '+') return 0; //预防” ++123"情况
if(noFlags == false && (str.find(' ') != -1)) return 0; //预防“ +0 123”之类的情况
str = str.substr(1);
}
if(str[0] == '-' && noFlags){ //负数
int i = 0, count = 0;
for(i = 1; i < str.length(); i++){
if(str[i] >= '0' && str[i] <= '9'){ //该字符是数字
count++;
if(count > 10) return INT_MIN;
else{
if(res > 2147483647/10) return INT_MIN;
if(res == 2147483647/10){
if(str[10] > '8') return INT_MIN;
}
res = res * 10 + (str[i]-'0');
}
}
else{ //该字符不是数字
break;
}
}
return 0-res;
}
else{ //正数
int i = 0, count = 0;
for(i = 0; i < str.length(); i++){
if(str[i] >= '0' && str[i] <= '9'){ //该字符是数字
count++; //统计字数
if(count > 10){ //数越界
return INT_MAX;
}
else{
if(res > 2147483647/10) return INT_MAX;
if(res == 2147483647/10){
if(str[9] > '7') return INT_MAX;
}
res = res*10 + (str[i]-'0');
}
}
else{ //该字符不是数字
break;
}
}
return res;
}
}
};
int main(){
Solution s;
string str;
// str = ""; // 0
// str = "123"; // 123
// str = "1234567890123456"; // 2147483647
// str = "2147483646"; //2147483646
// str = "2147483647"; //2147483647
// str = "2147483648" ; //2147483647
// str = "1234567890123460"; // 2147483647
// str = "1234567890#12"; //1234567890
// str = "12345678#12"; // 12345678
// str = "12345678901245#12"; // 214783647
// str = "123456&12" // 123456
// str = "&12"; // 0
// str = "-123456"; // -123456
// str = "-1234567890123456"; // -2147483648
// str = "-2147483647"; //-2147483647
// str = "-2147483648"; //-2147483648
// str = "-2147483649"; //-2147483648
// str = "+123"; //123;
// str = " 010"; //10
// str = "+-2"; //0
// str = "++2"; //0
// str = "-+2"; //0
// str = "--0"; //0
// str = " +0 123"; //0
// str = " +0123"; //123
// str = " + 1 123"; //0
str = " +004500"; //4500
cout<<s.myAtoi(str)<<endl;
return 0;
}