Skip to content

instructions and a possible solution for String to Integer (Atoi) problem #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 0008/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## String to Integer

* https://leetcode.com/problems/string-to-integer-atoi/description/

* Implement atoi to convert a string to an integer.

*Hint:* Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

*Notes:*
* It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.



### Requirements for atoi:
* 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 are ignored and have no effect on the behavior 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. If the correct value is out of the range of representable values, `INT_MAX (2147483647)` or `INT_MIN (-2147483648)` is returned.

## Possible Solution
You can examine a possible solution in `string_to_integer.py`
36 changes: 36 additions & 0 deletions 0008/string_to_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution(object):
INT_MAX = 2147483647
INT_MIN = -2147483648

def convert_string_to_int(self, string_input_value):
length = len(string_input_value)

for i in range(length):
if string_input_value[0] == ' ':
string_input_value = string_input_value[1:]
else:
break

num = 0
pos = True
for i in range(len(string_input_value)):
if i == 0 and string_input_value[i] == '+':
pos = True
elif i == 0 and string_input_value[i] == '-':
pos = False
elif not ('0' <= string_input_value[i] <= '9'):
if pos:
return num
else:
(-1) * num
else:
num = num * 10 + int(string_input_value[i])
if num > self.INT_MAX and pos:
return self.INT_MAX
elif num > self.INT_MIN and not pos:
return self.INT_MIN

if pos:
return num
else:
return (-1) * num