当前位置:网站首页>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;
}
}
边栏推荐
- It is difficult to quantify the extent to which a single-chip computer can find a job
- LeetCode - 705 设计哈希集合(设计)
- LeetCode - 900. RLE 迭代器
- QT self drawing button with bubbles
- 2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
- Swing transformer details-1
- Timer and counter of 51 single chip microcomputer
- 01仿B站项目业务架构
- Application of external interrupts
- Wireshark use
猜你喜欢

Yocto technology sharing phase IV: customize and add software package support

CV learning notes convolutional neural network

Dictionary tree prefix tree trie

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn

LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))

LeetCode - 900. RLE 迭代器

Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*

Pycharm cannot import custom package

LeetCode - 715. Range 模块(TreeSet) *****
随机推荐
Opencv Harris corner detection
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
Swing transformer details-2
Leetcode - 933 number of recent requests
The data read by pandas is saved to the MySQL database
Markdown latex full quantifier and existential quantifier (for all, existential)
(2)接口中新增的方法
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
. DLL and Differences between lib files
CV learning notes - camera model (Euclidean transformation and affine transformation)
Circular queue related design and implementation reference 1
使用sed替换文件夹下文件
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Opencv note 21 frequency domain filtering
is_ power_ of_ 2 judge whether it is a multiple of 2
51 MCU tmod and timer configuration
El table X-axis direction (horizontal) scroll bar slides to the right by default
Not many people can finally bring their interests to college graduation
Yocto Technology Sharing Phase 4: Custom add package support
Development of intelligent charging pile (I): overview of the overall design of the system