当前位置:网站首页>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;
}
}
边栏推荐
- Tensorflow built-in evaluation
- Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
- CV learning notes - camera model (Euclidean transformation and affine transformation)
- CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
- LeetCode - 673. Number of longest increasing subsequences
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- Opencv notes 17 template matching
- 01仿B站项目业务架构
- CV learning notes - scale invariant feature transformation (SIFT)
- LeetCode - 5 最长回文子串
猜你喜欢
使用密钥对的形式连接阿里云服务器
yocto 技術分享第四期:自定義增加軟件包支持
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
Leetcode-106:根据中后序遍历序列构造二叉树
Leetcode bit operation
CV learning notes - scale invariant feature transformation (SIFT)
Opencv feature extraction - hog
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
CV learning notes convolutional neural network
LeetCode - 900. RLE 迭代器
随机推荐
2021-10-27
使用密钥对的形式连接阿里云服务器
Leetcode-106:根据中后序遍历序列构造二叉树
Leetcode 300 longest ascending subsequence
(2) New methods in the interface
Cases of OpenCV image enhancement
Not many people can finally bring their interests to college graduation
One click generate traffic password (exaggerated advertisement title)
Opencv feature extraction - hog
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Opencv gray histogram, histogram specification
CV learning notes - camera model (Euclidean transformation and affine transformation)
(2)接口中新增的方法
Connect Alibaba cloud servers in the form of key pairs
Yocto technology sharing phase IV: customize and add software package support
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
(1) What is a lambda expression
Yocto Technology Sharing Phase 4: Custom add package support
When the reference is assigned to auto
Dynamic layout management