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

解题:

这题是栈的一个经典应用,也比较简单,思路就是:遇到数字就进栈,遇到运算符号就出栈两个数字然后再将计算结果进栈。

代码:

import java.util.Stack;

public class LeetCode150_EvaluateReversePolishNotation {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] s={"4","-2","/","2","-3","-","-"};
		System.out.println(evalRPN(s));

	}

	 public static int evalRPN(String[] tokens) {
		 int length=tokens.length;
		 Stack<Integer> stack =new Stack<>();

		 for(int i=0;i<length;i++)
		 {
			 if(!isOperator(tokens[i]))
				 stack.push(Integer.parseInt(tokens[i]));
			 else {
				int operaNum2=stack.pop();
				int operaNum1=stack.pop();
				if(tokens[i].equals("+"))
					stack.push(operaNum1+operaNum2);
				else if(tokens[i].equals("-"))
					stack.push(operaNum1-operaNum2);
				else if(tokens[i].equals("*"))
					stack.push(operaNum1*operaNum2);
				else {
					stack.push(operaNum1/operaNum2);
				}
			}

		 }

		 return stack.pop();

	    }

	 public static boolean isOperator(String s)
	 {
		 if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))
			 return true;
		 else
			 return false;
	 }

}

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

时间: 2024-10-09 01:50:22

LeetCode150 Evaluate Reverse Polish Notation java题解的相关文章

【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

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

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

Evaluate Reverse Polish Notation leetcode 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 Evaluate Reverse Polish Notation(逆波兰表达式)

表达式:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9   ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 题目大意:给定一个逆波兰表达式,求该表达式的值 思路:由于逆波兰表达式本身

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】:Evaluate Reverse Polish Notation (python)

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