Skip to content

Commit afe46d7

Browse files
authored
Add Python3 solution to README_EN.md
1 parent 0f385a7 commit afe46d7

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,32 @@ tags:
7171
#### Python3
7272

7373
```python
74-
74+
class Solution:
75+
def calculate(self, s: str) -> int:
76+
def dfs(q):
77+
num, sign, stk = 0, "+", []
78+
while q:
79+
c = q.popleft()
80+
if c.isdigit():
81+
num = num * 10 + int(c)
82+
if c == "(":
83+
num = dfs(q)
84+
if c in "+-*/)" or not q:
85+
match sign:
86+
case "+":
87+
stk.append(num)
88+
case "-":
89+
stk.append(-num)
90+
case "*":
91+
stk.append(stk.pop() * num)
92+
case "/":
93+
stk.append(int(stk.pop() / num))
94+
num, sign = 0, c
95+
if c == ")":
96+
break
97+
return sum(stk)
98+
99+
return dfs(deque(s))
75100
```
76101

77102
#### Java

0 commit comments

Comments
 (0)