当前位置:网站首页>逆波兰表达式
逆波兰表达式
2022-07-05 12:39:00 【厚积薄发ض】
栈的应用之逆波兰表达式
什么是逆波兰表达式呢??-->来源力扣150. 逆波兰表达式求值
逆波兰表达式:
逆波兰表达式是一种后缀表达式,所谓后缀就是指算符写在后面。
平常使用的算式则是一种中缀表达式,如 ( 1 + 2 ) * ( 3 + 4 ) 。
该算式的逆波兰表达式写法为 ( ( 1 2 + ) ( 3 4 + ) * ) 。
逆波兰表达式主要有以下两个优点:去掉括号后表达式无歧义,上式即便写成 1 2 + 3 4 + * 也可以依据次序计算出正确结果。
适合用栈操作运算:遇到数字则入栈;遇到算符则取出栈顶两个数字进行计算,并将结果压入栈中
也就是说我们平常写的对 数的加法减法乘法除法,都是这样的..的例如: ( 1 + 2 ) * ( 3 + 4 )-->中缀表达式,而我们这样能让计算机读懂呢??->这就引入了今天的逆波兰表达式也就是后缀表达式,我们就让中缀表达式转成后缀表达式。
例如:1+2*3+(4*2+5)*6

- 将所有运算都加上大括号
- 将运算符移动到对应括号的外面
- 去掉所有括号
好了我们就得到了一个后缀表达式-->也就是计算机能读懂的计算
那计算机是怎么通过这样的后缀表达式来计算出结果呢???
那就是我们这样神奇的数据结构-->栈

- 遇到数字就加入到栈中
- 遇到运算符就出两个数字
- 第一次出的放在运算符右边,第二次出的放运算符左边
- 计算完成之后继续放入栈中
- 最终栈顶元素就是最终表达式计算的结果
代码实现:
class Solution {
public int evalRPN(String[] tokens) {
//思路:遇到数字就入栈,如果遇到数字符号就出两个数进行计算
//计算结果继续入栈,一直遍历字符串结束
Stack<Integer> stack = new Stack<>();
for(int i =0;i<tokens.length;++i){
String str = tokens[i];
if(!isNumCharacter(str)){//判断是否是符号
//如果是数字的话就将其入栈
int num = Integer.parseInt(str);
stack.push(num);
}else{
//如果是字符就弹栈,弹出两个数字
int num2 = stack.pop();
int num1 = stack.pop();
switch(str){
case "+":
stack.push(num1+num2);
break;
case "-":
stack.push(num1-num2);
break;
case "*":
stack.push(num1*num2);
break;
case "/":
stack.push(num1/num2);
break;
}
}
}
return stack.peek();
}
//用来判断这个字符串是否是字符
public boolean isNumCharacter(String s){
if(s.equals("+")||s.equals("*")||
s.equals("-")||s.equals("/")){
return true;
}
return false;
}
}边栏推荐
- Kotlin函数
- Distributed solution - completely solve website cross domain requests
- Making and using the cutting tool of TTF font library
- Neural network of PRML reading notes (1)
- Docker configures redis and redis clusters
- Lepton 无损压缩原理及性能分析
- Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
- I met Tencent in the morning and took out 38K, which showed me the basic smallpox
- Pytoch through datasets Imagefolder loads datasets directly from files
- 10 minute fitness method reading notes (5/5)
猜你喜欢

A deep long article on the simplification and acceleration of join operation

关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益

RHCAS6

What is the difference between Bi software in the domestic market

Kotlin variable

激动人心!2022开放原子全球开源峰会报名火热开启!

Volatile instruction rearrangement and why instruction rearrangement is prohibited

太方便了,钉钉上就可完成代码发布审批啦!

【Nacos云原生】阅读源码第一步,本地启动Nacos
![[Nacos cloud native] the first step of reading the source code is to start Nacos locally](/img/f8/d9b848593cf7380a6c99ee0a8158f8.png)
[Nacos cloud native] the first step of reading the source code is to start Nacos locally
随机推荐
Insmod prompt invalid module format
Preliminary exploration of basic knowledge of MySQL
How can labels/legends be added for all chart types in chart. js (chartjs.org)?
Super efficient! The secret of swagger Yapi
谈谈我写作生涯的画图技巧
Simply take stock reading notes (4/8)
【云原生】Nacos中的事件发布与订阅--观察者模式
Add a new cloud disk to Huawei virtual machine
Kotlin variable
SAP UI5 视图里的 OverflowToolbar 控件
自然语言处理系列(一)入门概述
Halcon 模板匹配实战代码(一)
Redis cluster configuration
Four common problems of e-commerce sellers' refund and cash return, with solutions
Sqoop import and export operation
#yyds干货盘点#js截取文件后缀名
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
VoneDAO破解组织发展效能难题
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
Taobao short videos are automatically released in batches without manual RPA open source