-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.py
40 lines (28 loc) · 992 Bytes
/
atoi.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
29
30
31
32
33
34
35
36
37
38
39
40
class Solution:
def myAtoi(self, str: str) -> int:
str = str.strip();
output = 0;
negative = False;
if not str:
return 0;
if str[0]=='+':
negative = False;
elif str[0]=="-":
negative = True;
elif not str[0].isnumeric():
return 0;
else:
output = ord(str[0])-ord("0");
for i in range(1,len(str)):
if str[i].isnumeric():
output = output*10+(ord(str[i])-ord("0"));
if negative and output>=2147483648:
return -2147483648;
if not negative and output>=2147483648:
return 2147483647;
else:
break;
if negative == False:
return output;
else:
return -output;