当前位置:网站首页>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;
}
}
边栏推荐
- 2021-11-11 standard thread library
- yocto 技術分享第四期:自定義增加軟件包支持
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- LeetCode - 919. Full binary tree inserter (array)
- Basic use and actual combat sharing of crash tool
- LeetCode - 705 设计哈希集合(设计)
- The data read by pandas is saved to the MySQL database
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- Basic knowledge of communication interface
- Do you understand automatic packing and unpacking? What is the principle?
猜你喜欢

Leetcode-513:找树的左下角值

RESNET code details

Leetcode-106:根据中后序遍历序列构造二叉树

1. Finite Markov Decision Process

CV learning notes - reasoning and training

LeetCode - 919. Full binary tree inserter (array)

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

openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹

3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation

2021-10-27
随机推荐
Yocto technology sharing phase IV: customize and add software package support
Pymssql controls SQL for Chinese queries
使用密钥对的形式连接阿里云服务器
Wireshark use
STM32 general timer output PWM control steering gear
Stm32 NVIC interrupt priority management
Interruption system of 51 single chip microcomputer
RESNET code details
LeetCode - 919. 完全二叉树插入器 (数组)
(1) What is a lambda expression
One click generate traffic password (exaggerated advertisement title)
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Timer and counter of 51 single chip microcomputer
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
CV learning notes - edge extraction
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
Development of intelligent charging pile (I): overview of the overall design of the system