当前位置:网站首页>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;
}
}
边栏推荐
- 3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
- 2.Elment Ui 日期选择器 格式化问题
- When the reference is assigned to auto
- Qcombox style settings
- 4G module at command communication package interface designed by charging pile
- STM32 general timer output PWM control steering gear
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- Pymssql controls SQL for Chinese queries
- RESNET code details
- Opencv image rotation
猜你喜欢

LeetCode - 715. Range 模块(TreeSet) *****

QT is a method of batch modifying the style of a certain type of control after naming the control

Opencv note 21 frequency domain filtering

CV learning notes - clustering

Working mode of 80C51 Serial Port

Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*

03 fastjason solves circular references

Yocto technology sharing phase IV: customize and add software package support
随机推荐
Connect Alibaba cloud servers in the form of key pairs
CV learning notes - feature extraction
LeetCode - 900. RLE 迭代器
01 business structure of imitation station B project
CV learning notes - reasoning and training
Markdown latex full quantifier and existential quantifier (for all, existential)
. DLL and Differences between lib files
When the reference is assigned to auto
Interruption system of 51 single chip microcomputer
(2)接口中新增的方法
Crash工具基本使用及实战分享
My 4G smart charging pile gateway design and development related articles
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
STM32 running lantern experiment - library function version
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
Google browser plug-in recommendation
Sending and interrupt receiving of STM32 serial port
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
YOLO_ V1 summary