-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.string-to-integer-atoi.py
58 lines (50 loc) · 1.57 KB
/
08.string-to-integer-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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# https://leetcode-cn.com/problems/string-to-integer-atoi/
import re
class Solution1:
'''
Date: 2022.04.06
Pass/Error/Bug: 1/3/1
执行用时: 48 ms, 在所有 Python3 提交中击败了 19.83% 的用户
内存消耗:15.0 MB, 在所有 Python3 提交中击败了 39.27% 的用户
'''
def myAtoi(self, s: str) -> int:
mtag = 0 # flag for plus or minus
ntag = 0 # flag for number start
ns = '0' # output
for sub_s in s:
if ntag * mtag == 0:
if sub_s == ' ':
continue
elif sub_s == '+':
ntag = 1
mtag = 1
continue
elif sub_s == '-':
mtag = -1
ntag = 1
continue
if sub_s in '0123456789.':
mtag = 1 if mtag != -1 else -1
ntag = 1
ns += sub_s
else:
break
ns = mtag * int(float(ns))
if ns > 2**31-1:
return 2**31-1
if ns < -2**31:
return -2**31
return ns
class Solution2:
'''
Date: 2022.04.06
Pass/Error/Bug: 1/2/1
执行用时: 44 ms, 在所有 Python3 提交中击败了 41.37% 的用户
内存消耗:14.8 MB, 在所有 Python3 提交中击败了 94.41% 的用户
'''
def myAtoi(self, s: str) -> int:
ns = re.findall(r'^[\+\-]?\d+', s.lstrip(' '))
if ns:
return max(min(int(ns[0]), 2**31-1), -2**31)
else:
return 0