当前位置:网站首页>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;
}
}边栏推荐
- Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
- MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
- Transactions from December 27 to 28, 2021
- PyCharm安装第三方库图解
- 将函数放在模块中
- 《信息系统项目管理师》备考笔记---信息化知识
- A small talk caused by the increase of sweeping
- Insmod prompt invalid module format
- Distance measuring sensor chip 4530a used in home intelligent lighting
- Setting up sqli lab environment
猜你喜欢

How to protect user privacy without password authentication?

Talk about my drawing skills in my writing career

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

国内市场上的BI软件,到底有啥区别

初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。

SAP UI5 DynamicPage 控件介绍

解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107

Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function

我在滴滴做开源

2021-12-21 transaction record
随机推荐
About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5
MySQL 巨坑:update 更新慎用影响行数做判断!!!
155. Minimum stack
SAP UI5 DynamicPage 控件介紹
2021.12.16-2021.12.20 empty four hand transaction records
155. 最小栈
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
谈谈我写作生涯的画图技巧
你的下一台电脑何必是电脑,探索不一样的远程操作
Didi open source Delta: AI developers can easily train natural language models
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
Free testing of Taobao tmall API order and flag insertion remark interface
奔跑,开路
Transactions from December 29, 2021 to January 4, 2022
Natural language processing series (I) introduction overview
NFT: how to make money with unique assets?
SAP UI5 FlexibleColumnLayout 控件介绍
函数传递参数小案例
自然语言处理系列(一)入门概述
Distance measuring sensor chip 4530a used in home intelligent lighting