当前位置:网站首页>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;}}} 
边栏推荐
- With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
- security CSRF漏洞保护
- 2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
- 【ACWing】230. 排列计数
- Win11如何获得最佳电源效率?
- asyncawait和promise的区别
- Zadig 面向开发者的自测联调子环境技术方案详解
- LeetCode_322_零钱兑换
- 【Leetcode】475. Heaters
- ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
猜你喜欢

async和await用法介绍

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

Work for 5 years, test case design is bad?To look at the big case design summary

工作5年,测试用例都设计不好?来看看大厂的用例设计总结

Axure tutorial - the new base (small white strongly recommended!!!)

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

How to solve the error when mysql8 installs make

如何进行数据库备份

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

WEB安全基础 - - - XRAY使用
随机推荐
电机原理动图合集
Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers
[Three sons] C language implements simple three sons
Axure教程-新手入门基础(小白强烈推荐!!!)
Docker搭建Mysql主从复制
在CentOS下安装MySQL
【MySQL系列】MySQL数据库基础
How to get the best power efficiency in Windows 11?
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
An interview question about iota in golang
thinkphp漏洞总结
检查 Oracle 版本的 7 种方法
els 方块变形判断。
Bean的生命周期
@WebServlet注解(Servlet注解)
ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
Work for 5 years, test case design is bad?To look at the big case design summary
为什么要使用MQ消息中间件?这几个问题必须拿下
@Resource和@Autowired的区别