-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08.py
More file actions
36 lines (31 loc) · 998 Bytes
/
08.py
File metadata and controls
36 lines (31 loc) · 998 Bytes
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
29
30
31
32
33
34
35
36
__author__ = 'alex'
class Solution(object):
def myAtoi(self, str):
result = 0
is_nagetive = False
space_end = False
sign_end = False
for index in range(0, len(str)):
if str[index].isdigit():
result = result * 10 + (ord(str[index]) - 48)
space_end = True
elif str[index] == " " and not space_end:
continue
elif str[index] == "+" and not sign_end:
sign_end = True
space_end = True
elif str[index] == "-" and not sign_end:
sign_end = True
space_end = True
is_nagetive = True
else:
break
if is_nagetive:
result *= -1
if result > 2147483647:
result = 2147483647
if result < -2147483648:
result = -2147483648
return result
solution = Solution()
print solution.myAtoi(" +004500")