-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_my_atoi.cpp
61 lines (55 loc) · 1.36 KB
/
5_my_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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Solution {
public:
string delSpace(const string &str){
unsigned int i, j;
string tmp("");
unsigned size = str.size();
for(i = 0; i < size && str[i] ==' '; i++);
if(str[i] == '-' || str[i] == '+')tmp += str[i++];
for(j = 0; i < size && str[i] >= '0' && str[i] <= '9'; j++,i++){
tmp += str[i];
}
return tmp;
}
int myAtoi(string str) {
long long res = 0;
int flag = 1;
string intmax("2147483647");
string intmin("2147483648");
string tmp("");
str = delSpace(str);
if(str[0] < '0'){
flag = str[0]=='-'?-1:1;
for(unsigned int i = 1; i < str.size(); i++)
{
tmp += str[i];
}
}
else {
tmp = str;
}
for(unsigned int j = 0; j < tmp.size(); j++){
res = res * 10 +(tmp[j]-'0');
if(flag == 1){
if(res > INT32_MAX)return INT32_MAX;
}
else{
if(res > 2147483648)return INT32_MIN;
}
}
res *= flag;
return res;
}
};
int main()
{
Solution s;
string str("-20000000000");
int res = s.myAtoi(str);
cout << res << endl;
return 0;
}