-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_string_to_integer
More file actions
38 lines (29 loc) · 878 Bytes
/
Copy path8_string_to_integer
File metadata and controls
38 lines (29 loc) · 878 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
37
38
class Solution:
def myAtoi(self, s) -> int:
s = s.strip()
if not s:
return 0
ints = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
str_result = ""
sign = 1
if s[0] == "+" or s[0] == "-":
if s[0] == "-":
sign = -1
s = s[1:]
for i in range(len(s)):
if s[i] in ints:
str_result += s[i]
else:
break
if str_result == "":
return 0
result = int(str_result) * sign
if result < -2**31:
return -2**31
if result > 2**31 - 1:
return 2**31 - 1
return result
testing = Solution()
print(testing.myAtoi("1337c0d3"))
#runtime: 0ms, beats 100%
#memory: 19.29MB, beats 83.91%