Implement A to I
Problem Statement:
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.
The algorithm for myAtoi(string s) is as follows:
- Whitespace: Ignore any leading whitespace (
" "). - Signedness: Determine the sign by checking if the next character is
'-'or'+', assuming positivity if neither present. - Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.
- Rounding: If the integer is out of the 32-bit signed integer range
[-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than231should be rounded to231, and integers greater than231 - 1should be rounded to231 - 1.
Return the integer as the final result.
-
Example:
✅ Solution: Recursion + Digit Extraction
class Solution {
public:
// Recursive helper to extract digits and build the number
void solve(string s, int ind, int n, bool isNeg, long long &result){
if(ind >= n || !isdigit(s[ind])) return;
int digit = s[ind] - '0';
result = result * 10 + digit;
// Clamp to prevent overflow
if(result > INT_MAX){
result = isNeg ? INT_MIN : INT_MAX;
return;
}
solve(s, ind + 1, n, isNeg, result);
}
int myAtoi(string s) {
int n = s.size();
int i = 0;
bool isNeg = false;
// Skip leading whitespaces
while(i < n && s[i] == ' '){
i++;
}
// Handle optional sign
if(i < n && (s[i] == '-' || s[i] == '+')){
isNeg = (s[i] == '-');
i++;
}
long long result = 0;
solve(s, i, n, isNeg, result);
// Apply sign
result = isNeg ? -result : result;
// Clamp result to 32-bit signed integer range
if(result < INT_MIN) return INT_MIN;
if(result > INT_MAX) return INT_MAX;
return result;
}
};
📝 How It Works
- This simulates
atoi()conversion:- Skip leading spaces.
- Handle optional sign (
+or ). - Parse digits recursively using
solve(). - Clamp result to 32-bit signed integer range.
- Recursive
solve()buildsresultdigit by digit while tracking overflow.
🧩 Key Logic
result = result * 10 + digit;
if(result > INT_MAX){
result = isNeg ? INT_MIN : INT_MAX;
return;
}
⏱️ Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time | O(N) ✅ |
| Space | O(N) due to recursion ✅ |
⚠️ Edge Cases
" "→ returns 0"+-12"→ returns 0"0032"→ returns 32"-91283472332"→ clamps to INT_MIN"words123"→ returns 0
💡 Other Approaches
| Method | Notes |
|---|---|
| Iterative Parsing | ✅ Most common and preferred |
| Recursive Parsing | ✅ Clean and conceptual |
| Regex Matching | ❌ Not efficient or readable |
🔁 Related Problems
💬