Skip to content

Commit d7e5e35

Browse files
authored
cleanup 772 syntax
1 parent ef684ed commit d7e5e35

File tree

1 file changed

+14
-17
lines changed
  • solution/0700-0799/0772.Basic Calculator III

1 file changed

+14
-17
lines changed

solution/0700-0799/0772.Basic Calculator III/README.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,27 @@ tags:
7676
class Solution:
7777
def calculate(self, s: str) -> int:
7878
def dfs(q):
79-
num, sign = 0, "+"
80-
stk = []
79+
num, sign, stk = 0, "+", []
8180
while q:
8281
c = q.popleft()
8382
if c.isdigit():
84-
num = num * 10 + int(c)
85-
if c == "(":
83+
num = num*10 + int(c)
84+
if c == '(':
8685
num = dfs(q)
87-
if c in "+-*/)" or not q:
88-
#! careful we catch ')' to eval (...), ow (4-5/2) will only be sum([4,-5]) => -1, /2 is ignored
89-
if sign == "+":
90-
stk.append(num)
91-
elif sign == "-":
92-
stk.append(-num)
93-
elif sign == "*":
94-
stk.append(stk.pop() * num)
95-
elif sign == "/":
96-
stk.append(int(stk.pop() / num))
97-
sign = c
98-
num = 0
86+
if c in '+-*/)' or not q:
87+
match sign:
88+
case "+":
89+
stk.append(num)
90+
case "-":
91+
stk.append(-num)
92+
case "*":
93+
stk.append(stk.pop()*num)
94+
case "/":
95+
stk.append(int(stk.pop()/num))
96+
num, sign = 0, c
9997
if c == ")":
10098
break
10199
return sum(stk)
102-
103100
return dfs(deque(s))
104101
```
105102

0 commit comments

Comments
 (0)