lintcode-medium-Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression inReverse Polish Notation.

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

["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
public class Solution {
    /**
     * @param tokens The Reverse Polish Notation
     * @return the value
     */
    public int evalRPN(String[] tokens) {
        // Write your code here

        if(tokens == null || tokens.length == 0)
            return 0;

        Stack<Integer> stack = new Stack<Integer>();

        for(int i = 0; i < tokens.length; i++){
            if(!isOp(tokens[i])){
                stack.push(Integer.parseInt(tokens[i]));
            }
            else{
                int num2 = stack.pop();
                int num1 = stack.pop();

                if(tokens[i].equals("+"))
                    stack.push(num1 + num2);
                else if(tokens[i].equals("-"))
                    stack.push(num1 - num2);
                else if(tokens[i].equals("*"))
                    stack.push(num1 * num2);
                else
                    stack.push(num1 / num2);
            }
        }

        return stack.pop();
    }

    public boolean isOp(String str){
        return (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/"));
    }
}
时间: 2024-08-28 23:09:14

lintcode-medium-Evaluate Reverse Polish Notation的相关文章

[LeetCode]Evaluate Reverse Polish Notation

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

[LeetCode][JavaScript]Evaluate Reverse Polish Notation

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

LeetCode: Evaluate Reverse Polish Notation 解题报告

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

leetcode - [2]Evaluate Reverse Polish Notation

Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expres

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

LeetCode: Reverse Words in a String: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&q

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

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

【leetcode】:Evaluate Reverse Polish Notation (python)

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

[leetcode]Evaluate Reverse Polish Notation @ Python

原题地址:https://oj.leetcode.com/problems/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

Evaluate Reverse Polish Notation (Python)

[问题] 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", "*"