-
Notifications
You must be signed in to change notification settings - Fork 2
/
q0227.py
47 lines (38 loc) · 1.12 KB
/
q0227.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
class Solution:
def calculate(self, s: str) -> int:
num1 = 0
num2 = 0
s = "+" + s.replace(" ","") + " "
length = len(s) - 1
i = 0
while i < length:
char = s[i]
if char == "+" or char == "-":
num1 += num2
tmp = 0
i += 1
while ord("0") <= ord(s[i]) <= ord("9"):
tmp = tmp * 10 + ord(s[i]) -48
i += 1
i -= 1
if char == "-":
num2 = -tmp
else:
num2 = tmp
elif char == "*" or char == "/":
tmp = 0
i += 1
while ord("0") <= ord(s[i]) <= ord("9"):
tmp = tmp * 10 + ord(s[i]) - 48
i += 1
i -= 1
if char == "*":
num2 *= tmp
else:
num2 = int(num2 / tmp)
i += 1
# print(char, num1, num2)
return num1 + num2
s = "3+2*2"
result = Solution().calculate(s)
print(result)