当前位置:网站首页>20220608 other: evaluation of inverse Polish expression
20220608 other: evaluation of inverse Polish expression
2022-07-03 10:11:00 【Seeyouagain】
Title Description : according to Reverse Polish notation , Find the value of the expression . Valid operators include +
、-
、*
、/
. Each operand can be an integer , It can also be another inverse Polish expression . Be careful The division between two integers retains only the integer part .
coded :
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;
}
}
边栏推荐
- Leetcode-112:路径总和
- CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
- pycharm 无法引入自定义包
- 2.1 Dynamic programming and case study: Jack‘s car rental
- QT is a method of batch modifying the style of a certain type of control after naming the control
- Label Semantic Aware Pre-training for Few-shot Text Classification
- YOLO_ V1 summary
- Implementation of "quick start electronic" window dragging
- . DLL and Differences between lib files
- Connect Alibaba cloud servers in the form of key pairs
猜你喜欢
CV learning notes - image filter
2.1 Dynamic programming and case study: Jack‘s car rental
Label Semantic Aware Pre-training for Few-shot Text Classification
Leetcode-513:找树的左下角值
LeetCode - 673. 最长递增子序列的个数
ADS simulation design of class AB RF power amplifier
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
yocto 技術分享第四期:自定義增加軟件包支持
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
Opencv histogram equalization
随机推荐
. DLL and Differences between lib files
使用sed替换文件夹下文件
CV learning notes alexnet
LeetCode - 673. 最长递增子序列的个数
CV learning notes - edge extraction
20220610其他:任务调度器
CV learning notes - deep learning
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
Opencv histogram equalization
CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
Serial port programming
LeetCode - 673. Number of longest increasing subsequences
Cases of OpenCV image enhancement
Problems encountered when MySQL saves CSV files
20220604数学:x的平方根
03 fastjason solves circular references
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Google browser plug-in recommendation
is_ power_ of_ 2 judge whether it is a multiple of 2
4.1 Temporal Differential of one step