-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-to-integer-atoi.cpp
73 lines (63 loc) · 1.58 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
/*
* Copyright (c) 2018 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <algorithm>
#include <array>
#include <climits>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
// https://leetcode.com/problems/string-to-integer-atoi
int myAtoi(string str) {
for (; str.length() > 0 && ' ' == str[0]; str = str.substr(1))
;
if (0 == str.length()) {
return 0;
}
int sign = '-' == str[0] ? -1 : +1;
if ('-' == str[0] || '+' == str[0]) {
str = str.substr(1);
}
if (str[0] < '0' || str[0] > '9') {
return 0;
}
array<uint8_t, 10> decimal_digits;
fill(decimal_digits.begin(), decimal_digits.end(), 0);
size_t highest_pos = 0;
for (size_t i = 0; str[0] >= '0' && str[0] <= '9';
str = str.substr(1), i++) {
if ('0' == str[0] && 0 == i) {
i--;
continue;
}
if (i >= decimal_digits.size()) {
if (sign > 0) {
return INT_MAX;
} else {
return INT_MIN;
}
}
decimal_digits[i] = str[0] - '0';
highest_pos = i;
}
reverse(decimal_digits.begin(), decimal_digits.begin() + highest_pos + 1);
int accumulator = 0;
for (size_t i = 0, fac = 1; i <= highest_pos; i++, fac *= 10) {
uint64_t diff = ((uint64_t(1) << 31) - 1) - accumulator;
uint64_t xx = decimal_digits[i] * fac;
if (xx > diff) {
if (sign > 0) {
return INT_MAX;
} else {
return INT_MIN;
}
}
accumulator += xx;
}
return accumulator * sign;
}
};