-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathString_to_Integer.py
28 lines (22 loc) · 1.1 KB
/
String_to_Integer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Question(#8): Given a string representing an integer, return that integer
# Solution: Parse the string and bulild up the number
def myAtoi(self, str: str) -> int:
# Firstly, strip the string of whitespaces, initialize variables to hold our number and the sign (positive or negative)
str = str.lstrip()
sign = 1
base = i = 0
if not str: return 0
# If the string starts with a sign, set out sign variable appropriately
if str[0] in '-+':
sign = -1 if str[0] == '-' else 1
str = str[1:]
while i < len(str):
# If we encounter something thats not a digit, we can break out of our loop
if not str[i].isdigit(): break
# Multiply the number by 10 and add out current digit (this is a very common technique for building numbers from strings)
base *= 10
base += int(str[i])
i += 1
if base*sign >= 2**31-1: return 2**31-1
if base*sign <= -2**31: return -2**31
return base * sign