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", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

Subscribe to see which companies asked this question.

题意:求逆波兰表达式的结果

public class Solution {
    public int isopnd(String s){
        if(s.length()!=1)return 0;
            char c=s.charAt(0);
            switch(c){
                case ‘+‘: return 1;
                case ‘-‘: return 2;
                case ‘*‘: return 3;
                case ‘/‘: return 4;
                default:return 0;
            }
        }
    public int evalRPN(String[] tokens) {
        if(tokens.length==1) return Integer.valueOf(tokens[0]);
        Stack<Integer> stack=new Stack<Integer>();
        int ans=0;
        for(String tk : tokens){
            int op=isopnd(tk);
            if(op!=0){
            int a=stack.peek();
            stack.pop();
            int b=stack.peek();
            stack.pop();
            if(op==1){
                ans=a+b;
            }
            if(op==2){
                ans=b-a;
            }
            if(op==3){
                ans=a*b;
            }
            if(op==4){
                ans=b/a;
            }
            System.out.println(ans);
            stack.push(ans);
        }else{
            stack.push(Integer.valueOf(tk));
        }
        }
        return ans;
    }
}

PS:用栈。第一次调用栈。遇到数字,入栈,遇到运算符,出栈俩数字做计算,结果再入栈。

时间: 2024-11-16 05:50:57

Leetcode 448. 150. Evaluate Reverse Polish Notation JAVA语言的相关文章

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

【leetcode】:Evaluate Reverse Polish Notation (python)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的.然后进行相应的运算,将结果push进栈中. 这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题.这种不同表现在两个操作数符号不同时的情况. 在c 中 3 / -5 = 0,但是在python中, 结果却为 - 1.这种

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

LeetCode150 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 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

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