当前位置:网站首页>逆波兰表达式
逆波兰表达式
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;
}
}边栏推荐
- Taobao order amount check error, avoid capital loss API
- Kotlin process control and circulation
- Why is your next computer a computer? Explore different remote operations
- Using MySQL in docker
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
- HiEngine:可媲美本地的云原生内存数据库引擎
- Transactions on December 23, 2021
- Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
- 石臻臻的2021总结和2022展望 | 文末彩蛋
- Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
猜你喜欢

Pytoch implements tf Functions of the gather() function

Transactions from January 6 to October 2022

深度长文探讨Join运算的简化和提速

Super efficient! The secret of swagger Yapi

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

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

谈谈我写作生涯的画图技巧

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

Kotlin变量

CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
随机推荐
Principle of universal gbase high availability synchronization tool in Nanjing University
jxl笔记
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
Volatile instruction rearrangement and why instruction rearrangement is prohibited
【Nacos云原生】阅读源码第一步,本地启动Nacos
JDBC exercise - query data encapsulated into object return & simple login demo
GPON other manufacturers' configuration process analysis
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
SAP UI5 DynamicPage 控件介绍
Programming skills for optimizing program performance
How can labels/legends be added for all chart types in chart. js (chartjs.org)?
Neural network of PRML reading notes (1)
开发者,云原生数据库是未来吗?
What is the difference between Bi software in the domestic market
Pytoch through datasets Imagefolder loads datasets directly from files
Comprehensive upgrade of Taobao short video photosynthetic platform
RHCSA5
单独编译内核模块
Kotlin函数
石臻臻的2021总结和2022展望 | 文末彩蛋