-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathString to Integer (atoi).cpp
98 lines (92 loc) · 2.64 KB
/
String to Integer (atoi).cpp
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
//solution1:Handle overflow before adding new char; ans is "int",
class Solution {
public:
int myAtoi(string str) {
int i=0;
while(i<str.length() && str[i] == ' ') i++;
int ans = 0; bool negative = false;
if(i<str.length() && (str[i]=='-'|| str[i] == '+')){
negative = str[i] == '-'?true:false;
i++;
}
while(i<str.length()){
if(str[i]<'0' || str[i] > '9'){
break;
}
if(ans > 214748364){
return negative?INT_MIN:INT_MAX;
}else if(ans == 214748364 && str[i] >= '8'){
return negative?INT_MIN:INT_MAX;
}
ans = ans*10 + str[i] - '0';
i++;
}
return negative?-ans:ans;
}
};
//solution2: Handle overflow after adding new char; ans is "long long"
class Solution {
public:
int myAtoi(string str) {
int i=0;
while(i<str.length() && str[i] == ' ') i++;
long long ans = 0; bool negative = false;
if(i<str.length() && (str[i]=='-'|| str[i] == '+')){
negative = str[i] == '-'?true:false;
i++;
}
while(i<str.length()){
if(str[i]<'0' || str[i] > '9'){
break;
}
ans = ans*10 + str[i] - '0';
if(ans >= INT_MAX && !negative){
return INT_MAX;
}
if(ans > INT_MAX && negative){
return INT_MIN;
}
i++;
}
return negative?-ans:ans;
}
};
//solution3: handle overflow in advance, ans is 'int', process as negative
class Solution {
public:
//need to consider some special situation
//1. leading blank spaces --> just skip
//2. positive or negative sign
//3. non digit character --> return number before it
//4. overflow
//to deal with overflow:
//process as negative: num = num*10 - (str[i] - '0')
//check overflow in advance, if oveflow just return
//why process as negative, because INT_MIN's absolute value is larger by 1 than INT_MAX
int myAtoi(string str) {
int i = 0;
while(str[i] == ' ') i++;
int flag = -1;
if(str[i] == '+')
i++;
else if(str[i] == '-') {
flag = 1;
i++;
}
int overflow = flag < 0 ? INT_MAX : INT_MIN, ret = 0;
while('0' <= str[i] && str[i] <= '9') {
int val = str[i] - '0';
if(ret < INT_MIN/10) return overflow;
ret = ret*10;
if(ret < INT_MIN + val) return overflow;
ret = ret - val;
i++;
}
if(flag<0 && ret == INT_MIN) return overflow;
return ret*flag;
}
//test case:
//Input:"2147483648"
//Output:-2147483648
//Expected:2147483647
}