当前位置:网站首页>逆波兰表达式
逆波兰表达式
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;
}
}边栏推荐
- What is the difference between Bi software in the domestic market
- RHCSA5
- Distributed solution - distributed session consistency problem
- 《信息系统项目管理师》备考笔记---信息化知识
- insmod 提示 Invalid module format
- JDBC -- use JDBC connection to operate MySQL database
- SAP UI5 ObjectPageLayout 控件使用方法分享
- Redis master-slave configuration and sentinel mode
- Pytoch counts the number of the same elements in the tensor
- SAP 自开发记录用户登录日志等信息
猜你喜欢

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

A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this

I met Tencent in the morning and took out 38K, which showed me the basic smallpox

Comprehensive upgrade of Taobao short video photosynthetic platform

SAP self-development records user login logs and other information

关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论

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

Distance measuring sensor chip 4530a used in home intelligent lighting

2021-12-21 transaction record

【云原生】Nacos中的事件发布与订阅--观察者模式
随机推荐
CVPR 2022 | single step 3D target recognizer based on sparse transformer
HiEngine:可媲美本地的云原生内存数据库引擎
Compile kernel modules separately
Transactions from January 14 to 19, 2022
Common commands and basic operations of Apache Phoenix
Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
Simply take stock reading notes (4/8)
RHCSA1
Distance measuring sensor chip 4530a used in home intelligent lighting
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
RHCSA5
SAP UI5 视图里的 OverflowToolbar 控件
JDBC -- use JDBC connection to operate MySQL database
Time conversion error
谈谈我写作生涯的画图技巧
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
研究:数据安全工具在 60% 的情况下无法抵御勒索软件
mysql拆分字符串做条件查询
Super efficient! The secret of swagger Yapi
自然语言处理系列(一)入门概述