-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-8.cpp
More file actions
31 lines (28 loc) · 963 Bytes
/
LeetCode-8.cpp
File metadata and controls
31 lines (28 loc) · 963 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
/*************************************************************************
> File Name: LeetCode-8.cpp
> Author: ltw
> Mail: 3245849061@qq.com
> Created Time: Tue 19 May 2020 07:59:12 PM CST
************************************************************************/
#include <iostream>
using namespace std;
class Solution {
public:
int myAtoi(string str) {
int flag = 1, num = 0, i = 0, pre_max = INT_MAX / 10, d = INT_MAX % 10;
while (str[i] == ' ') ++i;
if (str[i] == '-') flag = -1, ++i;
else if (str[i] == '+') flag = 1, ++i;
while (str[i]) {
if (str[i] < '0' || str[i] >'9') break;
//防止溢出逼出来的花招
if (num > pre_max
|| (num == pre_max && (str[i] - '0') > d)) {
return flag == 1 ? INT_MAX : INT_MIN;
}
num = num * 10 + (str[i] - '0');
i++;
}
return num * flag;
}
};