Skip to content

Latest commit

 

History

History
50 lines (39 loc) · 1.42 KB

_150. Evaluate Reverse Polish Notation.md

File metadata and controls

50 lines (39 loc) · 1.42 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 11, 2024

Last updated : July 01, 2024


Related Topics : Array, Math, Stack

Acceptance Rate : 53.45 %


Solutions

Python

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        vals = []

        for token in tokens :
            if token.isnumeric() or len(token) > 1:
                vals.append(token)
            else :
                val1, val2 = int(vals.pop()), int(vals.pop())
                match token :
                    case '+' :
                        vals.append(str(val2 + val1))
                    case '-' :
                        vals.append(str(val2 - val1))
                    case '*' :
                        vals.append(str(val2 * val1))
                    case '/' :
                        if (val1 > 0) == (val2 > 0) :
                            vals.append(str(val2 // val1))
                        else :
                            vals.append(str(-1 * (abs(val2) // abs(val1))))
        return int(vals.pop())