代码拉取完成,页面将自动刷新
package com.hit.basmath.learn.queue_stack;
import java.util.Stack;
/**
* 150. Evaluate Reverse Polish Notation
* <p>
* Evaluate the value of an arithmetic expression in Reverse Polish Notation.
* <p>
* Valid operators are +, -, *, /. Each operand may be an integer or another expression.
* <p>
* Note:
* <p>
* 1. Division between two integers should truncate toward zero.
* 2. The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.
* <p>
* Example 1:
* <p>
* Input: ["2", "1", "+", "3", "*"]
* Output: 9
* Explanation: ((2 + 1) * 3) = 9
* <p>
* Example 2:
* <p>
* Input: ["4", "13", "5", "/", "+"]
* Output: 6
* Explanation: (4 + (13 / 5)) = 6
* <p>
* Example 3:
* <p>
* Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
* Output: 22
* Explanation:
* ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
* = ((10 * (6 / (12 * -11))) + 17) + 5
* = ((10 * (6 / -132)) + 17) + 5
* = ((10 * 0) + 17) + 5
* = (0 + 17) + 5
* = 17 + 5
* = 22
*/
public class _150 {
public int evalRPN(String[] tokens) {
int a, b;
Stack<Integer> stack = new Stack<Integer>();
for (String token : tokens) {
switch (token) {
case "+":
stack.add(stack.pop() + stack.pop());
break;
case "/":
b = stack.pop();
a = stack.pop();
stack.add(a / b);
break;
case "*":
stack.add(stack.pop() * stack.pop());
break;
case "-":
b = stack.pop();
a = stack.pop();
stack.add(a - b);
break;
default:
stack.add(Integer.parseInt(token));
break;
}
}
return stack.pop();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。