当前位置:网站首页>逆波兰表达式
逆波兰表达式
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;
}
}边栏推荐
- 2021-12-21 transaction record
- SAP SEGW 事物码里的 ABAP Editor
- CVPR 2022 | single step 3D target recognizer based on sparse transformer
- Kotlin流程控制、循环
- VoneDAO破解组织发展效能难题
- Lepton 无损压缩原理及性能分析
- 【云原生】Nacos中的事件发布与订阅--观察者模式
- 关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益
- Transactions from December 27 to 28, 2021
- 谈谈我写作生涯的画图技巧
猜你喜欢

Didi open source Delta: AI developers can easily train natural language models

Detailed structure and code of inception V3

使用 jMeter 对 SAP Spartacus 进行并发性能测试

SAP UI5 ObjectPageLayout 控件使用方法分享

DNS的原理介绍

超高效!Swagger-Yapi的秘密

JDBC -- extract JDBC tool classes

非技术部门,如何参与 DevOps?

Compilation principle reading notes (1/12)

I met Tencent in the morning and took out 38K, which showed me the basic smallpox
随机推荐
2021-12-21 transaction record
[cloud native] event publishing and subscription in Nacos -- observer mode
奔跑,开路
Rasa Chat Robot Tutorial (translation) (1)
RHCSA1
Annotation problem and hidden Markov model
Four common problems of e-commerce sellers' refund and cash return, with solutions
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
单独编译内核模块
Principle of universal gbase high availability synchronization tool in Nanjing University
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
Simply take stock reading notes (4/8)
NFT: how to make money with unique assets?
Free testing of Taobao tmall API order and flag insertion remark interface
前几年外包干了四年,秋招感觉人生就这样了..
谈谈我写作生涯的画图技巧
How do e-commerce sellers refund in batches?
关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface