-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_to_integer.cpp
More file actions
134 lines (127 loc) · 3.42 KB
/
Copy pathstring_to_integer.cpp
File metadata and controls
134 lines (127 loc) · 3.42 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<string>
#include<climits>
using namespace std;
class Solution {
public:
int myAtoi(string str) {
int s_size = str.size();
if(0 == s_size)
{
return 0;
}
int result = 0;
bool minus_flag = false;
bool flag = false;
const char* data = str.data();
int i = 0;
int j = 0;
for(i = 0; i < s_size; i++)
{
//first find out first non-whitespace character
if(' ' != data[i])
{
break;
}
}
if('-' == data[i])
{
minus_flag = true;
flag = true;
}
else if('+' == data[i])
{
minus_flag = false;
flag = true;
}
else if(('0' > data[i]) || ('9' < data[i]))
{
return 0;
}
if(flag)
{
j = i + 1;
}
else
{
j = i;
}
//ignore 0 till a non-zero digit character
for(; j < s_size; j++)
{
if('0' != data[j])
{
break;
}
}
for(; j < s_size; j++)
{
if(('0' <= data[j]) && ('9' >= data[j]))
{
if (result > INT_MAX / 10 || (result == INT_MAX / 10 && data[j] - '0' > 7))
{
if (!minus_flag) return INT_MAX;
else return INT_MIN;
}
result = data[j] - '0' + result * 10;
//cout << result <<endl;
}
else
{
break;
}
}
if(minus_flag)
{
result = -1 * result;
}
return result;
}
};
int main(void)
{
string test1 = {"123"};
string test2 = {"-123"};
string test3 = {" "};
string test4 = {" 124"};
string test5 = {" -124"};
string test6 = {" - 125"};
string test7 = {" 125 "};
string test8 = {" 1 25"};
string test9 = {"xxxxx"};
string test10 = {"x126"};
string test11 = {"x-126"};
string test12 = {"1x26"};
string test13 = {"-x126"};
string test14 = {"-1x26"};
string test15 = {"0127"};
string test16 = {"-0127"};
string test17 = {"+0127"};
string test18 = {"0127000"};
string test19 = {"-0127000"};
string test20 = {" -0128xxx128000 "};
string test21 = {"2147483648"};
Solution solution;
cout << solution.myAtoi(test1) << endl;
cout << solution.myAtoi(test2) << endl;
cout << solution.myAtoi(test3) << endl;
cout << solution.myAtoi(test4) << endl;
cout << solution.myAtoi(test5) << endl;
cout << solution.myAtoi(test6) << endl;
cout << solution.myAtoi(test7) << endl;
cout << solution.myAtoi(test8) << endl;
cout << solution.myAtoi(test9) << endl;
cout << solution.myAtoi(test10) << endl;
cout << solution.myAtoi(test11) << endl;
cout << solution.myAtoi(test12) << endl;
cout << solution.myAtoi(test13) << endl;
cout << solution.myAtoi(test14) << endl;
cout << solution.myAtoi(test15) << endl;
cout << solution.myAtoi(test16) << endl;
cout << solution.myAtoi(test17) << endl;
cout << solution.myAtoi(test18) << endl;
cout << solution.myAtoi(test19) << endl;
cout << solution.myAtoi(test20) << endl;
cout << solution.myAtoi(test21) << endl;
return 0;
}