150. Evaluate Reverse Polish Notation QuestionEditorial Solution

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +-*/. Each operand may be an integer or another expression.

Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

stack只存数字,遇到operator则pop两个数字并进行计算,然后将结果存stack。最后stack的第一个元素就是答案。注意 - 与 / 的差别, 先pop的值在算符右边。
public int EvalRPN(string[] tokens) {
        Stack<int> stack = new Stack<int>();
        for(int i = 0;i< tokens.Count();i++)
        {
            if(tokens[i] == "+")
            {
                stack.Push(stack.Pop() + stack.Pop());
            }
            else if(tokens[i] == "-")
            {
                stack.Push(-1*stack.Pop() + stack.Pop());
            }
            else if(tokens[i] == "*")
            {
                stack.Push(stack.Pop() * stack.Pop());
            }
            else if(tokens[i] == "/")
            {
                int a = stack.Pop();
                int b = stack.Pop();

                stack.Push(b / a);
            }
            else
            {
                stack.Push(Int32.Parse(tokens[i]));
            }
        }
        return stack.Peek();
    }
时间: 2024-12-20 06:27:00

150. Evaluate Reverse Polish Notation QuestionEditorial Solution的相关文章

leetcode 150. Evaluate Reverse Polish Notation ------ java

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

[leedcode 150] Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

Leetcode 448. 150. Evaluate Reverse Polish Notation JAVA语言

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples:   ["2", "1", "+", "3", "*"] -

150. Evaluate Reverse Polish Notation(逆波兰表达式)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

力扣算法题—150. Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Note: Division between two integers should truncate toward zero. The given RPN expression

Java for LeetCode 150 Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

leetcode[150] Evaluate Reverse Polish Notation

逆波兰表示法,在维基百科here 一不小心就看到了维基上有说用栈处理.然后就用栈处理了. 需要注意的是,操作数前后不要弄错,stoi可以调用.它应该是在stdlib.h的头文件里,不过我在codeblock上试了不行. class Solution { public: int evalRPN(vector<string> &tokens) { int size = tokens.size(); if (size == 0) return 0; stack<int> sta;

150. Evaluate Reverse Polish Notation

https://leetcode.com/problems/evaluate-reverse-polish-notation/#/description RPN中文名字叫做逆波兰表示法,它的好处维基百科说了,就是不需要括号来表示运算的先后,直接根据式子本身就可以求解.解题思路就是维护一个栈,遇到数字就入栈,遇到操作符就两次出栈得到栈顶的两个操作数,运用操作符进行运算以后,再把结果入栈.直到式子结束,此时栈中唯一一个元素便是结果. 以上代码中有一个没有周全的地方是没有对逆波兰式错误的情况进行出错处

LeetCode: Evaluate Reverse Polish Notation [150]

[题目] Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"