-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8-string-to-integer-atoi.cpp
40 lines (33 loc) · 1.19 KB
/
8-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
// Title: String to Integer (atoi)
// Description:
// Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).
// Link: https://leetcode.com/problems/string-to-integer-atoi/
// Time complexity: O(n)
// Space complexity: O(1)
class Solution {
public:
int myAtoi(std::string s) {
int sign = +1;
std::int64_t value = 0;
auto it = s.begin();
// skip any leading spaces
while (it != s.end() && *it == ' ') ++it;
// read the optional sign
if (it != s.end()) {
if (*it == '+') {
sign = +1, ++it;
} else if (*it == '-') {
sign = -1, ++it;
}
}
// read the digits
while (it != s.end() && isdigit(*it)) {
// append the parsed digit
value = value * 10 + (*it - '0'), ++it;
// stop reading digits when the value is already too large
if (value > INT32_MAX) break;
}
// return the clamped result
return (int) std::clamp<std::int64_t>(sign * value, INT32_MIN, INT32_MAX);
}
};