-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0008_myAtoi.cc
65 lines (60 loc) · 1.18 KB
/
Problem_0008_myAtoi.cc
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
#include <iostream>
#include <string>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int myAtoi(string s)
{
int ret = 0, i, flag = 0;
for (i = 0; s[i] == ' '; i++)
; // 过滤空格
if (s[i] == '-') // 判断正负
{
flag = 1;
i++;
}
else if (s[i] == '+')
{
i++;
}
for (; s[i] >= '0' && s[i] <= '9'; i++) // 只处理数字
{
if (ret > (0x7fffffff - (s[i] - '0')) / 10) // 溢出处理
{
if (flag)
{
return 1 << 31; // 负溢出返回- 2^32
}
else
{
return ~(1 << 31); // 正溢出返回2^32 - 1
}
}
ret *= 10;
ret += (s[i] - '0');
}
if (flag)
{
ret = ~ret + 1; // 取负
}
return ret;
}
};
void testMyAtoi()
{
Solution s;
EXPECT_EQ_INT(42, s.myAtoi("42"));
EXPECT_EQ_INT(-42, s.myAtoi(" -42"));
EXPECT_EQ_INT(4193, s.myAtoi("4193 with words"));
EXPECT_EQ_INT(0, s.myAtoi("words and 987"));
EXPECT_EQ_INT(~(1 << 31), s.myAtoi("2147483648"));
EXPECT_EQ_INT(1 << 31, s.myAtoi("-2147483649"));
EXPECT_SUMMARY;
}
int main()
{
testMyAtoi();
return 0;
}