[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", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
public class Solution {
    //对于逆波兰式,一般都是用栈来处理,依次处理字符串,

    //如果是数值,则push到栈里面

    //如果是操作符,则从栈中pop出来两个元素,计算出值以后,再push到栈里面,

    //则最后栈里面剩下的元素即为所求。
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<Integer>();
        for(int i=0;i<tokens.length;i++){
            if(!isDigit(tokens[i])){
                stack.push(Integer.parseInt(tokens[i]));
                continue;
            }
            int num1=stack.pop();
            int num2=stack.pop();
            if(tokens[i].equals("-")){
                stack.push(num2-num1);
            }
            if(tokens[i].equals("+")){
                stack.push(num1+num2);
            }
            if(tokens[i].equals("*")){
                stack.push(num1*num2);
            }
            if(tokens[i].equals("/")){
                stack.push(num2/num1);
            }
        }
        return stack.pop();
    }
    public boolean isDigit(String s){
        if(s.equals("-")||s.equals("+")||s.equals("*")||s.equals("/")) return true;
        return false;
    }
}
时间: 2024-12-17 17:48:35

[leedcode 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

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;

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