当前位置:网站首页>20220608其他:逆波兰表达式求值
20220608其他:逆波兰表达式求值
2022-07-03 09:20:00 【丿SeeYouAgain】
题目描述:根据 逆波兰表示法,求表达式的值。有效的算符包括 +
、-
、*
、/
。每个运算对象可以是整数,也可以是另一个逆波兰表达式。注意 两个整数之间的除法只保留整数部分。
编码实现:
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String token : tokens) {
if (noDigit(token)) {
Integer b = stack.pop();
Integer a = stack.pop();
stack.push(calc(a, b, token));
continue;
}
stack.push(Integer.parseInt(token));
}
return stack.peek();
}
private boolean noDigit(String s) {
return "+".equals(s) || "-".equals(s) || "*".equals(s) || "/".equals(s);
}
private int calc(int a, int b, String c) {
switch (c) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
return a / b;
default:
return 0;
}
}
边栏推荐
- Serial communication based on 51 single chip microcomputer
- Window maximum and minimum settings
- [combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
- LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
- Application of external interrupts
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- Tensorflow built-in evaluation
- (2)接口中新增的方法
- Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
- CV learning notes - clustering
猜你喜欢
Leetcode 300 最长上升子序列
RESNET code details
1. Finite Markov Decision Process
03 fastjason solves circular references
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
Opencv note 21 frequency domain filtering
QT is a method of batch modifying the style of a certain type of control after naming the control
LeetCode - 919. Full binary tree inserter (array)
Yocto technology sharing phase IV: customize and add software package support
2021-10-27
随机推荐
Vgg16 migration learning source code
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
el-table X轴方向(横向)滚动条默认滑到右边
QT is a method of batch modifying the style of a certain type of control after naming the control
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
yocto 技术分享第四期:自定义增加软件包支持
Liquid crystal display
【C 题集】of Ⅵ
Modelcheckpoint auto save model
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
Circular queue related design and implementation reference 1
4G module IMEI of charging pile design
Serial port programming
03 fastjason solves circular references
CV learning notes - image filter
Opencv feature extraction - hog
Working mode of 80C51 Serial Port
CV learning notes - reasoning and training
Markdown latex full quantifier and existential quantifier (for all, existential)