当前位置:网站首页>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;}}} 
边栏推荐
猜你喜欢

@Resource和@Autowired的区别

Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)

Classical Literature Reading--DLO

技术分享 | 接口测试中如何使用Json 来进行数据交互 ?

学习笔记:机器学习之回归

中缀转后缀、前缀表达式快速解决办法

利用“栈”快速计算——逆波兰表达式

OpenCV DNN blogFromImage()详解

Share an interface test project (very worth practicing)

Appears in oozie on CDH's hue, error submitting Coordinator My Schedule
随机推荐
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
@Transactional注解在类上还是接口上使用,哪种方式更好?
【三子棋】C语言实现简易三子棋
security CSRF Vulnerability Protection
根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
@WebServlet注解(Servlet注解)
DVWA靶场环境搭建
The Spark of Sql join on the and and where
【Leetcode】473. Matchsticks to Square
2022 6th Strong Net Cup Part WP
如何进行数据库备份
DOM 基础操作
Deliver cloud-native microservices applications with Zadig
QML包管理
Excel导入和导出
1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?
一个有些意思的项目--文件夹对比工具(一)
Use Jenkins for continuous integration, this knowledge point must be mastered
Docker搭建Mysql主从复制
好好活就是做有意义的事,有意义的事就是好好活