class Solution:
def calculate(self, s: str) -> int:
sta = []
s = s.replace(" ", "")
def calc(num, op):
if op == '+':
sta.append(num)
elif op == '-':
sta.append(-num)
elif op == '*':
sta.append(sta.pop() * num)
else:
sta.append(int(sta.pop() / num))
num, operator = 0, '+'
for idx, val in enumerate(s):
if val in '+-*/':
calc(num, operator)
num = 0
operator = val
else:
num = num * 10 + int(val)
calc(num, operator)
return sum(sta)
'Algrithm' 카테고리의 다른 글
LeetCode 128. Longest Consecutive Sequence (0) | 2023.02.26 |
---|---|
Leet Code 1834. Single-Threaded CPU (0) | 2023.02.19 |