[算法]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. For example:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

1. Naive Approach

This problem can be solved by using a stack. We can loop through each element in the given array. When it is a number, push it to the stack. When it is an operator, pop two numbers from the stack, do the calculation, and push back the result.

public class Test {

	public static void main(String[] args) throws IOException {
		String[] tokens = new String[] { "2", "1", "+", "3", "*" };
		System.out.println(evalRPN(tokens));
	}

	public static int evalRPN(String[] tokens) {
		int returnValue = 0;
		String operators = "+-*/";

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

		for (String t : tokens) {
			if (!operators.contains(t)) { //push to stack if it is a number
				stack.push(t);
			} else {//pop numbers from stack if it is an operator
				int a = Integer.valueOf(stack.pop());
				int b = Integer.valueOf(stack.pop());
				switch (t) {
				case "+":
					stack.push(String.valueOf(a + b));
					break;
				case "-":
					stack.push(String.valueOf(b - a));
					break;
				case "*":
					stack.push(String.valueOf(a * b));
					break;
				case "/":
					stack.push(String.valueOf(b / a));
					break;
				}
			}
		}

		returnValue = Integer.valueOf(stack.pop());

		return returnValue;
	}
}

or

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

        int returnValue = 0;

        String operators = "+-*/";

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

        for(String t : tokens){
            if(!operators.contains(t)){
                stack.push(t);
            }else{
                int a = Integer.valueOf(stack.pop());
                int b = Integer.valueOf(stack.pop());
                int index = operators.indexOf(t);
                switch(index){
                    case 0:
                        stack.push(String.valueOf(a+b));
                        break;
                    case 1:
                        stack.push(String.valueOf(b-a));
                        break;
                    case 2:
                        stack.push(String.valueOf(a*b));
                        break;
                    case 3:
                        stack.push(String.valueOf(b/a));
                        break;
                }
            }
        }

        returnValue = Integer.valueOf(stack.pop());

        return returnValue;

    }
}
时间: 2024-10-22 21:00:59

[算法]Evaluate Reverse Polish Notation的相关文章

[leecode]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 给出一个加减乘除的逆波兰式,求出它的结果: 什么是逆波兰式? 简单来说,逆波兰式就是表达式的后缀表示形式: 例如下面两个式子: ["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.这种