Skip to content

Commit e504968

Browse files
committed
Runtime: 137 ms (Top 57.95%) | Memory: 15.5 MB (Top 50.78%)
1 parent b8889df commit e504968

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

scripts/algorithms/B/Basic Calculator/Basic Calculator.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1+
# Runtime: 137 ms (Top 57.95%) | Memory: 15.5 MB (Top 50.78%)
12

23
```class Solution:
34
def calculate(self, s: str) -> int:
45
curr,output,sign,stack = 0,0,1,[]
5-
6+
67
for ch in s:
78
if ch.isdigit():
89
curr = curr * 10 + int(ch)
9-
10-
elif ch == '+':
11-
output += sign * curr
12-
sign = 1
10+
11+
elif ch == '+':
12+
output += sign * curr
13+
sign = 1
1314
curr = 0
14-
15+
1516
elif ch == '-':
1617
output += sign * curr
1718
sign = -1
18-
curr = 0
19-
19+
curr = 0
20+
2021
elif ch == '(':
2122
#push the result and then the sign
2223
stack.append(output)
2324
stack.append(sign)
2425
sign = 1
2526
output = 0
26-
27+
2728
elif ch == ')':
2829
output += sign * curr
2930
output *= stack.pop()

0 commit comments

Comments
 (0)