Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 150 · Expression Evaluation

Evaluate Reverse Polish Notation

LeetCode 150 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Operands on a stack; operator pops two.

நடுத்தரம்ArrayMathStack
LeetCode இல் திற ↗

நான் Evaluate Reverse Polish Notation (LeetCode 150) ஐ இப்படித் தீர்த்தேன் — Expression Evaluation பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

In RPN every operator applies to the two most recent values, so push numbers on a stack and, at an operator, pop two, combine, and push the result.

Approach

  1. 1

    Number → push.

  2. 2

    Operator → b = pop, a = pop, push a op b.

  3. 3

    Division truncates toward zero: int(a / b).

நான் எப்படித் தீர்த்தேன்

Stack evaluationPythonO(n) time
class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        #store the operation with operator as key in a map
        operations = {
            "*": lambda a,b:a*b,
            "+": lambda a,b:a+b,
            "-": lambda a,b:a-b,
            "/": lambda a,b:int(a/b)
        }
        stack = []
        for token in tokens:
            if token in operations:
                b = stack.pop()
                a = stack.pop()
                operation = operations[token]
                cur_val = operation(a,b)
                stack.append(cur_val)
            else:
                stack.append(int(token))
        return stack.pop()
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Evaluate Reverse Polish Notation — அடிக்கடி கேட்கப்படும் கேள்விகள்

Evaluate Reverse Polish Notation பின்னாலுள்ள intuition என்ன?

In RPN every operator applies to the two most recent values, so push numbers on a stack and, at an operator, pop two, combine, and push the result.

Evaluate Reverse Polish Notation (LeetCode 150) ஐ எப்படித் தீர்ப்பது?

1. Number → push. 2. Operator → b = pop, a = pop, push a op b. 3. Division truncates toward zero: int(a / b).

Evaluate Reverse Polish Notation இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(n). (Accepted approach: Stack evaluation.)

Evaluate Reverse Polish Notation இல் பொதுவான தவறுகள் என்ன?

Pop order matters for − and /. Python's // floors negatives; int(a / b) truncates as required.