当前位置:网站首页>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;}}}
边栏推荐
猜你喜欢
随机推荐
TCP 可靠吗?为什么?
els 方块变形判断。
如何重装Win11?一键重装Win11方法
Excel导入和导出
如何进行数据库备份
Axure tutorial - the new base (small white strongly recommended!!!)
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
【Leetcode】475. Heaters
如何设计循环队列?快进来学习~
Ansible中的任务执行控制
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
为什么要使用MQ消息中间件?这几个问题必须拿下
字节跳动面试官:请你实现一个大文件上传和断点续传
background-image使用
oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed
【Leetcode】2360. Longest Cycle in a Graph
Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程
如何优雅的消除系统重复代码
Unity—四元数、欧拉角API+坐标系统
Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers