[Leetcode]150. Evaluate Reverse Polish Notation
2024. 8. 1. 21:52ㆍAlgorithm
1. problem :
https://leetcode.com/problems/evaluate-reverse-polish-notation/
2. solution 1 :
from typing import List
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
operations = {
"+": lambda x, y: x + y,
"-": lambda x, y: x - y,
"*": lambda x, y: x * y,
"/": lambda x, y: int(x / y) # 수정된 부분: 정수 나눗셈
}
for token in tokens:
if token in operations:
temp1 = stack.pop()
temp2 = stack.pop()
operation = operations[token]
result = operation(temp2, temp1)
stack.append(result)
else:
stack.append(int(token))
return stack[0]
'Algorithm' 카테고리의 다른 글
[Leetcode]739. Daily Temperatures(x) (0) | 2024.08.02 |
---|---|
[Leetcode]22. Generate Parentheses(x) (0) | 2024.08.02 |
[Leetcode]155. Min Stack (0) | 2024.08.01 |
[Leetcode]20. Valid Parentheses (0) | 2024.08.01 |
[Leetcode]42. Trapping Rain Water(x) (0) | 2024.08.01 |