forked from cpselvis/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution008.cpp
More file actions
59 lines (52 loc) · 954 Bytes
/
solution008.cpp
File metadata and controls
59 lines (52 loc) · 954 Bytes
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
/**
* @file String to integer.
* cpselvis([email protected])
* 2016.7.30
*/
#include<iostream>
#include<ctype.h>
using namespace std;
class Solution {
public:
int myAtoi(string str) {
/* Filter whitespace.*/
int i = 0;
int sign = 1;
unsigned long long result = 0;
while (str[i] == ' ')
{
i ++;
}
/* Parse first non-whitespace character. */
if (str[i] != '+' && str[i] != '-' && !isdigit(str[i]))
{
return 0;
}
if (str[i] == '+')
{
sign = 1;
i ++;
}
else if (str[i] == '-')
{
sign = -1;
i ++;
}
while (isdigit(str[i]))
{
result = result * 10 + str[i] - '0';
i ++;
if (result > INT_MAX)
{
return (sign == 1) ? INT_MAX : INT_MIN;
}
}
return result * sign;
}
};
int main(int argc, char **argv)
{
Solution s;
string str = "18446744073709551617";
cout << s.myAtoi(str) << endl;
}