Repository navigation
Expand file tree
/
Copy path8.string-to-integer.cpp
More file actions
104 lines (101 loc) · 2.95 KB
/
Copy path8.string-to-integer.cpp
File metadata and controls
104 lines (101 loc) · 2.95 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
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
// Not solved.
class Solution
{
public:
int myAtoi(string str)
{
unsigned int num = 0;
bool negative = false;
bool valid = true;
string nums = "0123456789";
string::iterator it = str.begin();
bool overflow = false;
while (valid && it != str.end())
{
if (!negative && *it == '-')
{
if ((it - 1) >= str.begin() && *(it - 1) == '+')
{
valid = false;
num = 0;
break;
}
negative = true;
++it;
}
else if (valid && (*it == ' ' || (*it == '0' && num == 0)))
{
if (*it == '0' && (it + 1) != str.end() && *(it + 1) == ' ')
{
valid = false;
num = 0;
break;
}
++it;
}
else if (*it == '+')
{
if ((it - 1) >= str.begin() && *(it - 1) == '-')
{
valid = false;
num = 0;
break;
}
++it;
}
else if (valid && nums.find(*it) == string::npos)
{
valid = false;
break;
}
else
{
int prev = num;
num = num * 10 + ((int)*it - '0');
// cout << prev << " " << num << endl;
if (prev > num / 10 || num - 1 >= INT_MAX)
{
overflow = true;
break;
}
++it;
}
}
if (overflow && negative)
{
return INT_MIN;
}
else if (overflow && !negative)
{
return INT_MAX;
}
else
{
if (negative)
{
return num * -1;
}
}
return num;
}
};
int main()
{
Solution s;
cout << (s.myAtoi(" -42") == -42 ? "true" : "false") << endl;
cout << (s.myAtoi("4193 with words") == 4193 ? "true" : "false") << endl;
cout << (s.myAtoi("words and 987") == 0 ? "true" : "false") << endl;
cout << (s.myAtoi("-91283472332") == -2147483648 ? "true" : "false") << endl;
cout << (s.myAtoi("91283472332") == 2147483647 ? "true" : "false") << endl;
cout << (s.myAtoi("+-2") == 0 ? "true" : "false") << endl;
cout << (s.myAtoi("-000000000000001") == -1 ? "true" : "false") << endl;
cout << (s.myAtoi(" +0 123") == 0 ? "true" : "false") << endl;
cout << (s.myAtoi("010") == 10 ? "true" : "false") << endl;
cout << (s.myAtoi("2147483648") == INT_MAX ? "true" : "false") << endl;
cout << (s.myAtoi("-6147483648") == INT_MIN ? "true" : "false") << endl;
return 0;
}