当前位置:网站首页>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;
}
}
边栏推荐
- 以VMware创新之道,重塑多云产品力
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验7 窗口看门狗实验(学习笔记)
- 【云原生】Nacos中的事件发布与订阅--观察者模式
- SAP SEGW 事物码里的 ABAP Editor
- Lepton 无损压缩原理及性能分析
- CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
- Natural language processing series (I) introduction overview
- Four common problems of e-commerce sellers' refund and cash return, with solutions
- 跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
- Simply take stock reading notes (2/8)
猜你喜欢
What if wechat is mistakenly sealed? Explain the underlying logic of wechat seal in detail
国内市场上的BI软件,到底有啥区别
Vonedao solves the problem of organizational development effectiveness
Talk about my drawing skills in my writing career
Kotlin variable
Taobao flag insertion remarks | logistics delivery interface
实战模拟│JWT 登录认证
ABAP editor in SAP segw transaction code
【云原生】Nacos-TaskManager 任务管理的使用
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
随机推荐
946. Verify stack sequence
SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子
Introduction aux contrôles de la page dynamique SAP ui5
It's too convenient. You can complete the code release and approval by nailing it!
DNS的原理介绍
RHCSA4
2021-12-21 transaction record
RHCSA3
Transactions from December 27 to 28, 2021
Taobao short video, why the worse the effect
Concurrent performance test of SAP Spartacus with JMeter
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
How to protect user privacy without password authentication?
SAP SEGW 事物码里的 ABAP Editor
Laravel document reading notes -mews/captcha use (verification code function)
SAP UI5 FlexibleColumnLayout 控件介绍
mysql拆分字符串做条件查询
《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
Rocky basics 1
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。