Skip to content

Commit b492eae

Browse files
committed
8. String to Integer (atoi) #array #java
1 parent 3381307 commit b492eae

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

problems/8/README.md

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

problems/8/StringToInteger.class

508 Bytes
Binary file not shown.

problems/8/StringToInteger.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.ArrayList;
2+
import java.util.Objects;
3+
4+
public class StringToInteger {
5+
public static int myAtoi(String s) {
6+
// definitions
7+
boolean numIsNegative = false;
8+
int counter = 0;
9+
10+
// ignore leading whitespace
11+
s = s.stripLeading();
12+
13+
if (Objects.equals(s, "")) {
14+
return 0;
15+
}
16+
17+
char[] charArray = s.toCharArray();
18+
if (charArray[0] == '-') {
19+
numIsNegative = true;
20+
counter = 1;
21+
} else if (charArray[0] == '+') {
22+
counter = 1;
23+
}
24+
25+
ArrayList<Integer> num = new ArrayList<Integer>();
26+
27+
for (; counter < charArray.length; counter++) {
28+
// check if the character is in int digit range from ascii table
29+
if (charArray[counter] - '0' > 9 || charArray[counter] - '0' < 0) {
30+
break;
31+
}
32+
num.add(charArray[counter] - '0');
33+
}
34+
35+
int finalNum = 0;
36+
int maxLimit = Integer.MAX_VALUE / 10;
37+
38+
for (int i = 0; i<num.size(); i++) {
39+
int tempNum = num.get(i);
40+
if(finalNum > maxLimit || (finalNum == maxLimit && tempNum > 7)){
41+
return numIsNegative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
42+
}
43+
if (tempNum != 0) {
44+
while (tempNum > 0) {
45+
finalNum *= 10;
46+
tempNum /= 10;
47+
}
48+
finalNum += num.get(i);
49+
} else {
50+
finalNum *= 10;
51+
}
52+
}
53+
54+
return numIsNegative ? -finalNum : finalNum;
55+
}
56+
57+
public static void main(String[] args) {
58+
System.out.println(myAtoi("42"));
59+
System.out.println(myAtoi("+42"));
60+
System.out.println(myAtoi(" -42"));
61+
System.out.println(myAtoi("004193 with words"));
62+
System.out.println(myAtoi("4193 with words"));
63+
System.out.println(myAtoi("21474836460"));
64+
System.out.println(myAtoi("2147483646")); // 2147483646
65+
}
66+
}

0 commit comments

Comments
 (0)