当前位置:网站首页>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;}}} 
边栏推荐
猜你喜欢
随机推荐
22.支持向量机—高斯核函数
20220725 Information update
根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
DOM 基础操作
学习笔记:机器学习之回归
Excel文件读写(创建与解析)
async/await 原理及执行顺序分析
Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
FAST-LIO2 code analysis (2)
洞见云原生微服务及微服务架构浅析
控制电机的几种控制电路原理图
How to solve the error when mysql8 installs make
GetHashCode方法与=
一个有些意思的项目--文件夹对比工具(一)
检查 Oracle 版本的 7 种方法
REST会消失吗?事件驱动架构如何搭建?
TexturePacker使用文档
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置









