-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet_0008_atoi.cpp
More file actions
49 lines (45 loc) · 1.42 KB
/
leet_0008_atoi.cpp
File metadata and controls
49 lines (45 loc) · 1.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
/* ******************************************** */
/* */
/* \\ /`/`` */
/* ~\o o_ 0 0\ */
/* / \__) (u ); _ _ */
/* / \/ \/ / \ \/ \/ \ */
/* /( . . ) ( )\ */
/* / \_____/ \_______/ \ */
/* [] [] [[] [[] *. */
/* []] []] [[] [[] */
/* */
/* ************************************ nuo *** */
class Solution
{
public:
int myAtoi(string s)
{
int sign, i;
long tt;
if ( !s.length()) return (0);
sign = 1;
tt = i = 0;
while (s[i] == ' ' || (s[i] < 14 && s[i] > 8))
i++;
if (s[i] == '+')
i++;
else if (s[i] == '-')
{
sign *= -1;
i++;
}
while (s[i] <= '9' && s[i] >= '0')
{
tt *= 10;
tt += s[i] - '0';
if (tt > 2147483647)
{
if (sign < 0) return (-2147483648);
return (2147483647);
}
i++;
}
return (tt * sign);
}
};