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

二、分析

这里的分析就援引百度文库对于逆波兰表达式的计算方法。

“它的优势在于只用两种简单操作,入栈和出栈就可以搞定任何普通表达式的运算。其运算方式如下:

如果当前字符为变量或者为数字,则压栈,如果是运算符,则将栈顶两个元素弹出作相应运算,结果再入栈,最后当表达式扫描完后,栈里的就是结果。”

三、代码(JAVA)

import java.util.Stack;
public class ReversePolishNotation {
	Stack<String> stack =new Stack();
public int evalRPN(String[] tokens) {
	if(tokens.length==0)
		return 0;
	int temp1,temp2,temp3;
	temp1=temp2=temp3=0;
    for(int i=0;i<tokens.length;i++){
    	if(tokens[i].equals("+")||
    	   tokens[i].equals("-")||
    	   tokens[i].equals("*")||
    	   tokens[i].equals("/")){
    		if(tokens.length==1)
    			return 0;
    		temp1=Integer.parseInt(stack.peek());
    		stack.pop();
    		temp2=Integer.parseInt(stack.peek());
    		stack.pop();
    		if(tokens[i].equals("+")){
    			temp3=temp1+temp2;
    			stack.push(Integer.toString(temp3));//计算结果同样要入栈
    		}
    		else if(tokens[i].equals("*")){
    			temp3=temp1*temp2;
    			stack.push(Integer.toString(temp3));
    		}
    		else if(tokens[i].equals("/")){
    			temp3=temp2/temp1;    //  注意temp2与temp1的顺序
    			stack.push(Integer.toString(temp3));
    		}
    		else if(tokens[i].equals("-")){
    			temp3=temp2-temp1;
    			stack.push(Integer.toString(temp3));
    		}
    	}
    	else{
    		temp3=Integer.parseInt(tokens[i]);//这里针对输入类似{“18”},既(s.length==1)&&(s[0]代表数字)
    		stack.push(tokens[i]);
    	}
    }
	return temp3;
    }
}
时间: 2024-08-05 11:17:37

【Leetcode】Evaluate Reverse Polish Notation答案的相关文章

[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

LeetCode: Evaluate Reverse Polish Notation [150]

[题目] 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 求算式值(AC)

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

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

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 计算逆波兰表达式

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 stack 栈

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

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

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