当前位置:网站首页>Using the "stack" fast computing -- reverse polish expression
Using the "stack" fast computing -- reverse polish expression
2022-08-02 00:15:00 【Chen Yikang】
Example: a b c * + d e * f + g * +
Corresponding data: 1 2 3 * + 4 5 * 6 + 7 * +
Analysis: (the data processing area is a virtual module)
The last element left on the stack at this point is the result of the expression
Example: Reverse Polish Expression Evaluation
Based on the above analysis, it will be much easier to do this question~ The code is as follows:
class Solution {public int evalRPN(String[] tokens) {Stack stack = new Stack<>();for(String str : tokens){if(judgment(str)){int x = stack.pop();int y = stack.pop();switch(str){case "+":stack.push(y + x);break;case "-":stack.push(y - x);break;case "*":stack.push(y * x);break;case "/":stack.push(y/x);break;}}else{stack.push(Integer.parseInt(str));}}return stack.pop();}private boolean judgment(String str){if(str.equals("+") || str.equals("-") ||str.equals("*") || str.equals("/")){return true;}else{return false;}}}
边栏推荐
- REST会消失吗?事件驱动架构如何搭建?
- @Transactional注解在类上还是接口上使用,哪种方式更好?
- Various Joins of Sql
- Several interview questions about golang concurrency
- security CSRF Vulnerability Protection
- 检查 Oracle 版本的 7 种方法
- Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
- CDH6 Hue to open a "ASCII" codec can 't encode characters
- TCL:在Quartus中使用tcl脚本语言进行管脚约束
- Use Jenkins for continuous integration, this knowledge point must be mastered
猜你喜欢
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
How to get the best power efficiency in Windows 11?
信息系统项目管理师必背核心考点(五十七)知识管理工具
【三子棋】C语言实现简易三子棋
How to reinstall Win11?One-click method to reinstall Win11
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
security跨域配置
如何设计循环队列?快进来学习~
@Resource和@Autowired的区别
TexturePacker使用文档
随机推荐
检查 Oracle 版本的 7 种方法
Enterprise firewall management, what firewall management tools are there?
字节跳动面试官:请你实现一个大文件上传和断点续传
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
Flink Yarn Per Job - Yarn应用
中缀转后缀、前缀表达式快速解决办法
很多人喜欢用多御安全浏览器,竟是因为这些原因
工作5年,测试用例都设计不好?来看看大厂的用例设计总结
【Leetcode】475. Heaters
oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed
Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程
面试必问的HashCode技术内幕
【MySQL篇】初识数据库
els 方块变形
REST会消失吗?事件驱动架构如何搭建?
Work for 5 years, test case design is bad?To look at the big case design summary
Docker搭建Mysql主从复制
WEB安全基础 - - - XRAY使用
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing