Skip to content

Commit 60c174a

Browse files
authored
Enhance readme with atoi problem solution
Added a detailed explanation and implementation for the String to Integer (atoi) problem in Java.
1 parent 37725d8 commit 60c174a

File tree

1 file changed

+71
-0
lines changed
  • src/main/java/g0001_0100/s0008_string_to_integer_atoi

1 file changed

+71
-0
lines changed

src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,77 @@ Return the integer as the final result.
8484

8585
Reading stops at the first non-digit character 'w'.
8686

87+
To solve the String to Integer (atoi) problem in Java using a `Solution` class, we'll follow these steps:
88+
89+
1. Define a `Solution` class with a method named `myAtoi`.
90+
2. Trim leading whitespace from the input string `s`.
91+
3. Check if the string is empty after trimming. If so, return 0.
92+
4. Initialize variables to keep track of the sign of the integer (`sign`), the starting index of the numeric characters (`start`), and the result (`result`).
93+
5. Check if the first character of the trimmed string is `'-'` or `'+'`. Update `sign` accordingly, and move the starting index accordingly.
94+
6. Iterate through the characters of the trimmed string starting from the `start` index:
95+
- Check if the current character is a digit. If not, break the loop.
96+
- Convert the character to its numeric value and update the `result`.
97+
- Check if the result exceeds the 32-bit integer range. If so, clamp it to the range and return.
98+
7. Multiply the `result` by the sign and return the final value.
99+
8. Handle edge cases where the input string is empty, consists of only whitespace, or contains non-numeric characters at the beginning.
100+
101+
Here's the implementation:
102+
103+
```java
104+
public class Solution {
105+
106+
public int myAtoi(String s) {
107+
s = s.trim();
108+
if (s.isEmpty())
109+
return 0;
110+
111+
int sign = 1;
112+
int start = 0;
113+
long result = 0;
114+
115+
if (s.charAt(0) == '-' || s.charAt(0) == '+') {
116+
sign = (s.charAt(0) == '-') ? -1 : 1;
117+
start++;
118+
}
119+
120+
for (int i = start; i < s.length(); i++) {
121+
char c = s.charAt(i);
122+
if (!Character.isDigit(c))
123+
break;
124+
result = result * 10 + (c - '0');
125+
if (result * sign > Integer.MAX_VALUE)
126+
return Integer.MAX_VALUE;
127+
if (result * sign < Integer.MIN_VALUE)
128+
return Integer.MIN_VALUE;
129+
}
130+
131+
return (int) (result * sign);
132+
}
133+
134+
public static void main(String[] args) {
135+
Solution solution = new Solution();
136+
137+
// Test cases
138+
String s1 = "42";
139+
System.out.println("Example 1 Output: " + solution.myAtoi(s1));
140+
141+
String s2 = " -42";
142+
System.out.println("Example 2 Output: " + solution.myAtoi(s2));
143+
144+
String s3 = "4193 with words";
145+
System.out.println("Example 3 Output: " + solution.myAtoi(s3));
146+
147+
String s4 = "words and 987";
148+
System.out.println("Example 4 Output: " + solution.myAtoi(s4));
149+
150+
String s5 = "-91283472332";
151+
System.out.println("Example 5 Output: " + solution.myAtoi(s5));
152+
}
153+
}
154+
```
155+
156+
This implementation provides a solution to the String to Integer (atoi) problem in Java.
157+
87158
**Constraints:**
88159

89160
* `0 <= s.length <= 200`

0 commit comments

Comments
 (0)