LeetCode_Stack_Evaluate Reverse Polish Notation

150. Evaluate Reverse Polish Notation


1. 问题描述:

有一种叫波兰表示法,它是将操作符至于操作数之前,那么这里是反过来,操作数在操作符之前。

输入是String数组,要求输出最后的计算结果。

2. 解决思路:

我们使用stack这种数据结构就很容易实现。栈中存放操作数,碰到操作符,即回去取栈顶的元素计算,结果再放回栈中,最后返回栈顶值即是。这里没有说计算无效或者计算式错误,比如除数为0的情况返回什么,所以就不做特殊处理。

3. java代码:

public class Solution {
    public int evalRPN(String[] tokens) {

        Stack<String> stack = new Stack<String>();
        for(int i=0;i<tokens.length; i++){
            String token = tokens[i];
            if(!isOperator(token)){
                stack.push(token);
            } else {
                int secondNum = Integer.parseInt(stack.pop());
                int firstNum = Integer.parseInt(stack.pop());
                int curResult = 0;
                 if(token.equals("+")) {
                    curResult = firstNum + secondNum;
                } else if(token.equals("/")) {
                    if(secondNum==0)
                        curResult = Integer.MAX_VALUE;
                    else
                        curResult = firstNum / secondNum;

                } else if(token.equals("-")) {
                    curResult = firstNum - secondNum;
                } else if(token.equals("*")) {
                    curResult = firstNum * secondNum;
                }
                stack.push(Integer.toString(curResult));
            }
        }

        return Integer.parseInt(stack.pop());
    }

    private boolean isOperator(String token){
        if(token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/"))
            return true;
        return false;
    }
}

4. 算法评估:



希望多多指正交流。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-28 09:11:25

LeetCode_Stack_Evaluate Reverse Polish Notation的相关文章

leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation

leetcode 逆波兰式求解 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", "

[LeetCode]Evaluate Reverse Polish Notation

题目:Evaluate Reverse Polish Notation 给出一个加减乘除的逆波兰式,求出它的结果: 什么是逆波兰式? 简单来说,逆波兰式就是表达式的后缀表示形式: 例如下面两个式子: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", &quo

LeetCode150 Evaluate Reverse Polish Notation

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

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. Example ["2", "1", "+", "3", "*"] -> ((2

lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]

Evaluate Reverse Polish Notation——LeetCode

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

[Leetcode] 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", "*"] -> (

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