150. Evaluate Reverse Polish Notation

https://leetcode.com/problems/evaluate-reverse-polish-notation/#/description

RPN中文名字叫做逆波兰表示法,它的好处维基百科说了,就是不需要括号来表示运算的先后,直接根据式子本身就可以求解。解题思路就是维护一个栈,遇到数字就入栈,遇到操作符就两次出栈得到栈顶的两个操作数,运用操作符进行运算以后,再把结果入栈。直到式子结束,此时栈中唯一一个元素便是结果。

以上代码中有一个没有周全的地方是没有对逆波兰式错误的情况进行出错处理,其实也不难,就是每次pop操作检查栈空情况,如果栈空,则说明出错,throw an exception。还有就是最后检查一下栈的size,如果不是1也说明运算数多了,返回错误。

public class Solution {
    public int evalRPN(String[] tokens) {
        if (tokens==null || tokens.length==0) return 0;
        LinkedList<Integer> stack = new LinkedList<Integer>();
        for (int i=0; i<tokens.length; i++) {
            if (tokens[i].equals("+") || tokens[i].equals("-") || tokens[i].equals("*") || tokens[i].equals("/")) {
                if (stack.isEmpty()) return 0;
                int operand1 = stack.pop();
                if (stack.isEmpty()) return 0;
                int operand2 = stack.pop();
                if (tokens[i].equals("+")) stack.push(operand2 + operand1);
                if (tokens[i].equals("-")) stack.push(operand2 - operand1);
                if (tokens[i].equals("*")) stack.push(operand2 * operand1);
                if (tokens[i].equals("/")) stack.push(operand2 / operand1);
            }
            else {
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        return stack.peek();
    }
}

  

时间: 2024-08-05 06:49:20

150. Evaluate Reverse Polish Notation的相关文章

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

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", "*"] -&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", "*"] -

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

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

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;

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", "*"