πͺ‘8. String to Integer (atoi)
Medium | Grind 75 | February 27th Monday, 2023
Implement the myAtoi(string s)
function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi
function).
The algorithm for myAtoi(string s)
is as follows:
Read in and ignore any leading whitespace.
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.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.
Convert these digits into an integer (i.e.
"123" -> 123
,"0032" -> 32
). If no digits were read, then the integer is0
. Change the sign as necessary (from step 2).If the integer is out of the 32-bit signed integer range
[-2^31, 2^31 - 1]
, then clamp the integer so that it remains in the range. Specifically, integers less than-2^31
should be clamped to-2^31
, and integers greater than2^31 - 1
should be clamped to2^31 - 1
.Return the integer as the final result.
Note:
Only the space character
' '
is considered a whitespace character.Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
Constraints:
0 <= s.length <= 200
s
consists of English letters (lower-case and upper-case), digits (0-9
),' '
,'+'
,'-'
, and'.'
.
Input: s = "42"
Output: 42
Explanation: The underlined characters are what is read in, the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
^
Step 3: "42" ("42" is read in)
^
The parsed integer is 42.
Since 42 is in the range [-231, 231 - 1], the final result is 42.
SOLUTION
Trim any leading whitespace characters from the start of the input string.
Check if the first character of the string is a sign symbol ('+' or '-').
If it is a sign, save the sign to a separate variable and update the input string to exclude the sign character.
Initialize a result variable to '0'.
Iterate through each character in the input string and examine it as follows:
If the character is a digit (0 to 9), add it to the result variable.
Since we only need to check for digits, it is more efficient to use a set of digit characters instead of a built-in function.
If the character is not a digit, stop the iteration.
Once the iteration is complete, convert the result variable to an integer.
If a negative sign was saved earlier, multiply the result by -1.
Return the final result.
class Solution:
def myAtoi(self, s: str) -> int:
# remove all leading white space
s = s.lstrip(' ')
if not s: return 0
# check if the first char is a sign
negative = False
if s[0] == '-' or s[0] == '+':
negative = s[0] == '-'
# remove the char that was just read
s = s[1:]
result = "0"
digits = set({"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"})
# read the string
for char in s:
if char in digits:
result += char
else:
break
result = int(result)*-1 if negative else int(result)
result = min(max(result, -2**31), 2**31-1)
return result
TIME AND SPACE COMPLEXITY
TIME: O(n) where n is the length of the string s. List comprehension of creating a string without the first characters takes O(n). The iteration of string s at most takes n times.
SPACE: O(n) where n is the length of the string s. The result variable will at most hold n characters like that of the original string s.
**written with help from ChatGPT
Last updated