Maintenance Mode

Have Patience Working on it

String to Integer

LEETCODE PROBLEM 8


Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which is ignored and have no effect on the behaviour of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
  • Only the space character ' ' is considered a whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1:
Input: "42"
Output: 42
Example 2:
Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.
SOLUTION: There are so many edge cases that are explained in the comments

class Solution(object):
def myAtoi(self, s):
s = s.strip() # remove white spaces from string " -42 " => "-42"
# need to track for +ve or -ve number
negative = False
# string can be empty also after removing space it can be ""
# that is why checked for string existance also
if s and s[0] == "-":
negative = True # number is -ve
# extract rest of string after +ve or -ve sign
if s and (s[0] == "+" or s[0] == "-"):
s = s[1:] # skipped sign
# string can be only sign also "-" , check for existance also
if not s: # empty string
return 0
# converting string to interger
# created set of digits because it provides O(1) search
digits = {i for i in "0123456789"}
result = 0 # answer
for c in s:
if c not in digits: # "1234 words" handling this edge case
break # hands "words and 987" this case will return 0
# 1 => 1 * 10 + 4 => 14 .....
# int() convert string "1" to integer 1
result = result * 10 + int(c)
# now i got number and check for -ve
if negative: # True
result = -result
# last case range
# −2**31, 2**31 − 1
# check result with max to get min value if result exceed 2**31 get 2**31
# after check with -2**31 to get which is max if result is less than 2**31
result = max(min(result, 2**31 - 1))
return result
o = Solution()
print(o.myAtoi(" 42 "),)
print(o.myAtoi(" -42 "))
print(o.myAtoi(" "))
print(o.myAtoi(" 2345 words "))
print(o.myAtoi("words 98765"))
print(o.myAtoi("09876545678987675667898787568"))
view raw StringToInt.py hosted with ❤ by GitHub
https://leetcode.com/problems/string-to-integer-atoi/
String to Integer String to Integer Reviewed by Leaf Code on August 08, 2020 Rating: 5

No comments:

SET ADT Using Students courses example

Powered by Blogger.