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

认识USB、Type-C、闪电、雷电接口

12306抢票,极限并发带来的思考?

使用 Zadig 交付云原生微服务应用

Study Notes: The Return of Machine Learning

cdh6 opens oozieWeb page, Oozie web console is disabled.

如何重装Win11?一键重装Win11方法

Arduino 基础语法

Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程

带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?

Axure tutorial - the new base (small white strongly recommended!!!)
随机推荐
为什么要使用MQ消息中间件?这几个问题必须拿下
Use Jenkins for continuous integration, this knowledge point must be mastered
ROS 动态参数
windows sql server 如何卸载干净?
Cash Ⅱ LeetCode_518_ change
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
机器学习文本分类
07-SDRAM :FIFO控制模块
EasyExcel的简单读取操作
在CentOS下安装MySQL
async/await 原理及执行顺序分析
20220725 Information update
ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
不就是个TCC分布式事务,有那么难吗?
Thymeleaf简介
@Transactional 注解使用详解
ansible模块--copy模块
根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程
els 方块变形判断。