当前位置:网站首页>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;
}
}
边栏推荐
- 20220602数学:Excel表列序号
- Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
- Design of charging pile mqtt transplantation based on 4G EC20 module
- Installation and removal of MySQL under Windows
- The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
- openCV+dlib實現給蒙娜麗莎換臉
- . DLL and Differences between lib files
- LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- Toolbutton property settings
猜你喜欢

Pycharm cannot import custom package

Opencv notes 17 template matching

El table X-axis direction (horizontal) scroll bar slides to the right by default

3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation

LeetCode - 5 最长回文子串

Octave instructions

Retinaface: single stage dense face localization in the wild

1. Finite Markov Decision Process

『快速入门electron』之实现窗口拖拽

Leetcode-513:找树的左下角值
随机推荐
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
2021-10-27
4G module board level control interface designed by charging pile
Wireshark use
Cases of OpenCV image enhancement
CV learning notes ransca & image similarity comparison hash
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Installation and removal of MySQL under Windows
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Stm32 NVIC interrupt priority management
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
Opencv notes 20 PCA
Crash工具基本使用及实战分享
20220531数学:快乐数
2.2 DP: Value Iteration & Gambler‘s Problem
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Label Semantic Aware Pre-training for Few-shot Text Classification
Tensorflow built-in evaluation