Java for LeetCode 150 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
解题思路:

计算逆波兰表达式,Stack的经典应用,JAVA实现如下:

	public int evalRPN(String[] tokens) {
		if (tokens == null || tokens.length == 0)
			return 0;
		Stack<Integer> stack = new Stack<Integer>();
		for (String str : tokens) {
			if (str.length() > 1 || Character.isDigit(str.charAt(0)))
				stack.push(Integer.parseInt(str));
			else {
				int a = stack.pop();
				int b = stack.pop();
				if (str.equals("+"))
					stack.push(b + a);
				else if (str.equals("-"))
					stack.push(b - a);
				else if (str.equals("*"))
					stack.push(b * a);
				else if (str.equals("/"))
					stack.push(b / a);
			}
		}
		return stack.pop();
	}
时间: 2024-10-11 02:36:45

Java for LeetCode 150 Evaluate Reverse Polish Notation的相关文章

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[150] Evaluate Reverse Polish Notation

逆波兰表示法,在维基百科here 一不小心就看到了维基上有说用栈处理.然后就用栈处理了. 需要注意的是,操作数前后不要弄错,stoi可以调用.它应该是在stdlib.h的头文件里,不过我在codeblock上试了不行. class Solution { public: int evalRPN(vector<string> &tokens) { int size = tokens.size(); if (size == 0) return 0; stack<int> sta;

【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][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", "+&qu

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

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

150. Evaluate Reverse Polish Notation QuestionEditorial Solution

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