2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_150.java 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-10 21:34 . 优化题解
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();
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助