当前位置:网站首页>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;
}
}
边栏推荐
- QT setting suspension button
- 01 business structure of imitation station B project
- QT detection card reader analog keyboard input
- CV learning notes - camera model (Euclidean transformation and affine transformation)
- CV learning notes - reasoning and training
- LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
- The underlying principle of vector
- Design of charging pile mqtt transplantation based on 4G EC20 module
- Leetcode 300 longest ascending subsequence
猜你喜欢

CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)

Opencv histogram equalization

Working mode of 80C51 Serial Port

Connect Alibaba cloud servers in the form of key pairs

el-table X轴方向(横向)滚动条默认滑到右边

Timer and counter of 51 single chip microcomputer
![[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)](/img/19/5dc152b3fadeb56de50768561ad659.jpg)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)

CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)

Opencv image rotation

Leetcode-513:找树的左下角值
随机推荐
Application of external interrupts
LeetCode - 673. Number of longest increasing subsequences
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
20220609其他:多数元素
Crash工具基本使用及实战分享
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
(2)接口中新增的方法
(1) What is a lambda expression
yocto 技术分享第四期:自定义增加软件包支持
CV learning notes - edge extraction
Design of charging pile mqtt transplantation based on 4G EC20 module
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Serial communication based on 51 single chip microcomputer
LeetCode - 673. 最长递增子序列的个数
4G module initialization of charge point design
20220603数学:Pow(x,n)
el-table X轴方向(横向)滚动条默认滑到右边
4G module board level control interface designed by charging pile
Leetcode 300 最长上升子序列
Opencv note 21 frequency domain filtering