Skip to content

Commit 0849f4f

Browse files
authored
feat: add python3 solution to lc problem: No.0772 (#4083)
1 parent f4bcddb commit 0849f4f

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,32 @@ tags:
7373
#### Python3
7474

7575
```python
76-
76+
class Solution:
77+
def calculate(self, s: str) -> int:
78+
def dfs(q):
79+
num, sign, stk = 0, "+", []
80+
while q:
81+
c = q.popleft()
82+
if c.isdigit():
83+
num = num * 10 + int(c)
84+
if c == "(":
85+
num = dfs(q)
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
97+
if c == ")":
98+
break
99+
return sum(stk)
100+
101+
return dfs(deque(s))
77102
```
78103

79104
#### Java

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

+26-1
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def calculate(self, s: str) -> int:
3+
def dfs(q):
4+
num, sign, stk = 0, "+", []
5+
while q:
6+
c = q.popleft()
7+
if c.isdigit():
8+
num = num * 10 + int(c)
9+
if c == "(":
10+
num = dfs(q)
11+
if c in "+-*/)" or not q:
12+
match sign:
13+
case "+":
14+
stk.append(num)
15+
case "-":
16+
stk.append(-num)
17+
case "*":
18+
stk.append(stk.pop() * num)
19+
case "/":
20+
stk.append(int(stk.pop() / num))
21+
num, sign = 0, c
22+
if c == ")":
23+
break
24+
return sum(stk)
25+
26+
return dfs(deque(s))

0 commit comments

Comments
 (0)