Advertisement

String to Integer (atoi) - LeetCode 8 Solution

String to Integer (atoi) - Complete Solution Guide

String to Integer (atoi) is LeetCode problem 8, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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. R

Detailed Explanation

The problem asks us to implement a function, `myAtoi(string s)`, that converts a given string `s` into a 32-bit signed integer, following specific rules similar to the `atoi` function in C/C++. The function should first ignore any leading whitespace characters. Then, it should check for an optional sign character ('+' or '-'). After that, it should read in digits until a non-digit character is encountered. Finally, it must handle potential integer overflow by clamping the result to the 32-bit signed integer range [-2<sup>31</sup>, 2<sup>31</sup> - 1]. If no digits are read or if the string is empty after removing whitespace, the function should return 0.

Solution Approach

The solution involves iterating through the string and applying the rules outlined in the problem description. First, leading whitespace is skipped. Next, the sign is determined (defaulting to positive if no sign is present). Then, the algorithm iterates through the string, accumulating digits into a number. Before adding a digit to the number, it checks whether adding the digit would cause an integer overflow. If an overflow is detected, the function returns `INT_MAX` or `INT_MIN` accordingly. If the loop finishes without overflow, the resulting number, adjusted for the sign, is returned.

Step-by-Step Algorithm

  1. Step 1: Initialize index `i` to 0 and iterate through the string `s` to skip leading whitespace characters. Increment `i` until a non-whitespace character is found or the end of the string is reached.
  2. Step 2: Check for a sign character ('+' or '-') at the current index `i`. If a '+' is found, increment `i`. If a '-' is found, set the `sign` variable to -1 and increment `i`. If neither is found, the sign remains the default value of 1 (positive).
  3. Step 3: Initialize a variable `num` to 0 to store the resulting integer.
  4. Step 4: Iterate through the string starting from the current index `i` as long as the current character is a digit.
  5. Step 5: Inside the loop, convert the current digit character to an integer.
  6. Step 6: Before adding the new digit to `num`, check for potential integer overflow. If the current number `num` is greater than `INT_MAX // 10` (or `INT_MIN // 10`) or `num` is equal to `INT_MAX // 10` (or `INT_MIN // 10`) and the digit is greater than `INT_MAX % 10` (or `INT_MAX % 10 + 1` for negative numbers), return `INT_MAX` or `INT_MIN` based on the sign.
  7. Step 7: Update `num` by multiplying it by 10 and adding the current digit: `num = num * 10 + digit`.
  8. Step 8: Increment the index `i` to move to the next character.
  9. Step 9: After the loop finishes (either reaching the end of the string or encountering a non-digit character), return the final result as `sign * num`.

Key Insights

  • The problem is essentially string parsing with careful handling of edge cases and potential integer overflow.
  • We need to iterate through the string character by character, keeping track of the sign and the current number being built.
  • The most crucial part is to check for integer overflow before multiplying the current number by 10 and adding the new digit. We need to compare the current number with `INT_MAX // 10` (or `INT_MIN // 10`) to determine if multiplying by 10 would cause an overflow.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: String.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Databricks, Goldman Sachs, Google, Infosys, Meta, Microsoft, Niantic, Nvidia, Qualcomm, Uber, Valve, Yahoo.