Skip to content

Commit 4edda66

Browse files
solves 8: string to atoi
1 parent b22ccb4 commit 4edda66

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring) | [![Java](assets/java.png)](src/LongestPalindromicSubstring.java) | |
1818
| 6 | [Zigzag Conversion](https://leetcode.com/problems/zigzag-conversion) | [![Java](assets/java.png)](src/ZigZagConversion.java) | |
1919
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [![Java](assets/java.png)](src/ReverseInteger.java) [![Python](assets/python.png)](python/reverse_integer.py) | [![java-yt](assets/java-yt.png)](https://youtu.be/7bOhyl5lWjI) [![python-yt](assets/python-yt.png)](https://youtu.be/lmLG30TLcSg) |
20+
| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | [![Java](assets/java.png)](src/StringToIntegerAtoi.java) | |
2021
| 9 | [PalindromeNumber](https://leetcode.com/problems/palindrome-number/) | [![Java](assets/java.png)](src/PalindromeNumber.java) [![Python](assets/python.png)](python/palindrome_number.py) | |
2122
| 13 | [Roman To Integer](https://leetcode.com/problems/roman-to-integer/) | [![Java](assets/java.png)](src/RomanToInteger.java) [![Python](assets/python.png)](python/roman_to_integer.py) | [![java-yt](assets/java-yt.png)](https://youtu.be/BCue_mO_81A) [![python-yt](assets/python-yt.png)](https://youtu.be/8h_yGTNvKMA) |
2223
| 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | [![Java](assets/java.png)](src/LongestCommonPrefix.java) [![Python](assets/python.png)](python/longest_common_prefix.py) | |

Diff for: src/StringToIntegerAtoi.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// https://leetcode.com/problems/string-to-integer-atoi
2+
// T:O(|input|)
3+
// S:O(1)
4+
5+
public class StringToIntegerAtoi {
6+
public int myAtoi(String input) {
7+
int sign = 1;
8+
int result = 0;
9+
int index = 0;
10+
int n = input.length();
11+
12+
// Discard all spaces from the beginning of the input string.
13+
while (index < n && input.charAt(index) == ' ') {
14+
index++;
15+
}
16+
17+
// sign = +1, if it's positive number, otherwise sign = -1.
18+
if (index < n && input.charAt(index) == '+') {
19+
index++;
20+
} else if (index < n && input.charAt(index) == '-') {
21+
sign = -1;
22+
index++;
23+
}
24+
25+
// Traverse next digits of input and stop if it is not a digit
26+
while (index < n && Character.isDigit(input.charAt(index))) {
27+
int digit = input.charAt(index) - '0';
28+
29+
// Check overflow and underflow conditions.
30+
if ((result > Integer.MAX_VALUE / 10) ||
31+
(result == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10)) {
32+
// If integer overflowed return 2^31-1, otherwise if underflowed return -2^31.
33+
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
34+
}
35+
36+
// Append current digit to the result.
37+
result = 10 * result + digit;
38+
index++;
39+
}
40+
41+
// We have formed a valid number without any overflow/underflow.
42+
// Return it after multiplying it with its sign.
43+
return sign * result;
44+
}
45+
}

0 commit comments

Comments
 (0)