当前位置:网站首页>Reverse Polish notation
Reverse Polish notation
2022-07-05 13:02:00 【accumulate steadily ض】
The inverse Polish expression of stack application
What is the inverse Polish expression ??--> Source force buckle 150. Evaluate the inverse Polish expression
Reverse Polish notation :
The inverse Polish expression is a suffix expression , The so-called suffix means that the operator is written after .
The usual formula is a infix expression , Such as ( 1 + 2 ) * ( 3 + 4 ) .
The inverse Polish expression of the formula is written as ( ( 1 2 + ) ( 3 4 + ) * ) .
The inverse Polish expression has two main advantages :There is no ambiguity in the expression after removing the brackets , Even if the above formula is written as 1 2 + 3 4 + * You can also calculate the correct result according to the order .
It is suitable for stack operation : When it comes to numbers, it's on the stack ; When you encounter an operator, you take out two numbers at the top of the stack to calculate , And push the results into the stack
That is to say, we usually write correctly Addition, subtraction, multiplication and division of numbers , It's all like this .. For example : ( 1 + 2 ) * ( 3 + 4 )--> Infix expression , And we can make the computer understand ??-> This introduces today's inverse Polish expression, that is, the suffix expression , Let's turn infix expression into suffix expression .
for example :1+2*3+(4*2+5)*6

- Add curly braces to all operations
- Move the operator outside the corresponding bracket
- Remove all parentheses
Well, we get a suffix expression --> That is, the calculation that the computer can read
How does the computer calculate the result through such a suffix expression ???
That is our magical data structure --> Stack

- When encountering numbers, it will be added to the stack
- When you encounter an operator, you will get two numbers
- The first output is placed on the right side of the operator , The second time out is to the left of the release operator
- After the calculation, continue to put it on the stack
- The final stack top element is the result of the final expression calculation
Code implementation :
class Solution {
public int evalRPN(String[] tokens) {
// Ideas : When it comes to numbers, put them on the stack , If you encounter a number symbol, you will give two numbers to calculate
// Continue to stack the calculation results , Continue to traverse the end of the string
Stack<Integer> stack = new Stack<>();
for(int i =0;i<tokens.length;++i){
String str = tokens[i];
if(!isNumCharacter(str)){// Judge whether it is a symbol
// If it's a number, put it on the stack
int num = Integer.parseInt(str);
stack.push(num);
}else{
// If it's a character, pop the stack , Pop up two numbers
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();
}
// Used to determine whether this string is a character
public boolean isNumCharacter(String s){
if(s.equals("+")||s.equals("*")||
s.equals("-")||s.equals("/")){
return true;
}
return false;
}
}边栏推荐
- How can non-technical departments participate in Devops?
- Taobao flag insertion remarks | logistics delivery interface
- How to connect the API interface of Taobao open platform (super detailed)
- RHCSA7
- 将函数放在模块中
- Introduction aux contrôles de la page dynamique SAP ui5
- SAP UI5 视图里的 OverflowToolbar 控件
- 石臻臻的2021总结和2022展望 | 文末彩蛋
- Halcon template matching actual code (I)
- Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
猜你喜欢

滴滴开源DELTA:AI开发者可轻松训练自然语言模型

《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動

Vonedao solves the problem of organizational development effectiveness

Lepton 无损压缩原理及性能分析

《信息系统项目管理师》备考笔记---信息化知识

SAP self-development records user login logs and other information

About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5

I'm doing open source in Didi

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

Taobao short videos are automatically released in batches without manual RPA open source
随机推荐
Pinduoduo flag insertion remarks API
Concurrent performance test of SAP Spartacus with JMeter
##无监控,不运维,以下是监控里常用的脚本监控
SAP SEGW 事物码里的 Association 建模方式
Talk about my drawing skills in my writing career
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
Rocky基础知识1
SAP SEGW 事物码里的 ABAP Editor
Taobao order amount check error, avoid capital loss API
insmod 提示 Invalid module format
Taobao short video, why the worse the effect
#yyds干货盘点#js截取文件后缀名
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
解决uni-app配置页面、tabBar无效问题
155. Minimum stack
函数的默认参数&函数参数的多种方法
Super efficient! The secret of swagger Yapi
Transactions from December 27 to 28, 2021
From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code
Taobao flag insertion remarks | logistics delivery interface