8000 8. String to Integer (atoi) · nullskill/leetcode@e3dd267 · GitHub
[go: up one dir, main page]

Skip to content

Commit e3dd267

Browse files
committed
8. String to Integer (atoi)
1 parent 41fb88f commit e3dd267

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
int myAtoi(String s) {
3+
s = s.trim();
4+
5+
if (s.isEmpty) return 0;
6+
7+
bool isNegative = false;
8+
if (s[0] == '-') {
9+
isNegative = true;
10+
s = s.substring(1);
11+
} else if (s[0] == '+') {
12+
s = s.substring(1);
13+
}
14+
15+
String digits = '0123456789';
16+
int result = 0;
17+
18+
for (var i = 0; i < s.length; i++) {
19+
if (!digits.contains(s[i])) break;
20+
21+
result = result * 10 + int.parse(s[i]);
22+
23+
if (isNegative && -result < -2147483648) {
24+
return -2147483648;
25+
} else if (!isNegative && result > 2147483647) {
26+
return 2147483647;
27+
}
28+
}
29+
30+
return result *= isNegative ? -1 : 1;
31+
}
32+
}
33+
34+
void main(List<String> args) {
35+
print(Solution().myAtoi('-2147483648'));
36+
print(Solution().myAtoi('9223372036854775808'));
37+
print(Solution().myAtoi('words and 987'));
38+
print(Solution().myAtoi('42'));
39+
print(Solution().myAtoi('+-12'));
40+
print(Solution().myAtoi(' -42'));
41+
print(Solution().myAtoi('+4193 with words'));
42+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
8. String to Integer (atoi)
2+
https://leetcode.com/problems/string-to-integer-atoi/
3+
4+
Implement the myAtoi(string s) function, which converts a string
5+
to a 32-bit signed integer (similar to C/C++'s atoi function).
6+
7+
The algorithm for myAtoi(string s) is as follows:
8+
9+
Read in and ignore any leading whitespace.
10+
Check if the next character (if not already at the end of the string) is '-' or '+'.
11+
Read this character in if it is either. This determines if the final result is negative
12+
or positive respectively. Assume the result is positive if neither is present.
13+
Read in next the characters until the next non-digit character or the end of the input
14+
is reached. The rest of the string is ignored.
15+
Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32).
16+
If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
17+
If the integer is out of the 32-bit signed integer range [-231, 231 - 1],
18+
then clamp the integer so that it remains in the range. Specifically, integers less than -231
19+
should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.
20+
Return the integer as the final result.
21+
Note:
22+
23+
Only the space character ' ' is considered a whitespace character.
24+
Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
25+
26+
27+
Example 1:
28+
29+
Input: s = "42"
30+
Output: 42
31+
Explanation: The underlined characters are what is read in, the caret is the current reader position.
32+
Step 1: "42" (no characters read because there is no leading whitespace)
33+
^
34+
Step 2: "42" (no characters read because there is neither a '-' nor '+')
35+
^
36+
Step 3: "42" ("42" i EDBE s read in)
37+
^
38+
The parsed integer is 42.
39+
Since 42 is in the range [-231, 231 - 1], the final result is 42.
40+
41+
Example 2:
42+
43+
Input: s = " -42"
44+
Output: -42
45+
Explanation:
46+
Step 1: " -42" (leading whitespace is read and ignored)
47+
^
48+
Step 2: " -42" ('-' is read, so the result should be negative)
49+
^
50+
Step 3: " -42" ("42" is read in)
51+
^
52+
The parsed integer is -42.
53+
Since -42 is in the range [-231, 231 - 1], the final result is -42.
54+
55+
Example 3:
56+
57+
Input: s = "4193 with words"
58+
Output: 4193
59+
Explanation:
60+
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
61+
^
62+
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
63+
^
64+
Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit)
65+
^
66+
The parsed integer is 4193.
67+
Since 4193 is in the range [-231, 231 - 1], the final result is 4193.
68+
69+
70+
Constraints:
71+
72+
0 <= s.length <= 200
73+
s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.

0 commit comments

Comments
 (0)
0