当前位置:网站首页>[leetcode 150] evaluation of inverse Polish expression
[leetcode 150] evaluation of inverse Polish expression
2022-04-23 06:24:00 【Don't steal my energy】
Title Description
according to Reverse Polish notation , Find the value of the expression .
Valid operators include +、-、*、/ . Each operand can be an integer , It can also be another inverse Polish expression .
Be careful The division between two integers retains only the integer part .
It can be guaranteed that the given inverse Polish expression is always valid . let me put it another way , The expression always yields a valid number and does not have a divisor of 0 The situation of .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/evaluate-reverse-polish-notation
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Example 1:
Input :tokens = ["2","1","+","3","*"]
Output :9
explain : This formula is transformed into a common infix arithmetic expression as :((2 + 1) * 3) = 9
Example 2:
Input :tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output :22
explain : This formula is transformed into a common infix arithmetic expression as :
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Ideas
Using the idea of stacks , Push non operators, that is, numbers, onto the stack , Every time you encounter an operator , Then take out the two elements at the top of the stack and calculate , Then press the calculation result into the stack , Until the traversal is finished , Finally, there is only one element left in the stack , This element is the result of the calculation . It will take advantage of stoi function .
//stoi Use of functions take string Convert to corresponding integer
cout<<stoi("123");//123
cout<<stoi("12a3");//12
cout<<stoi("a12");//error
Code :
int evalRPN(vector<string>& tokens) {
stack<int> tmp;
int a,b,c;
for(auto x:tokens)
{
if(x!="+" && x!="-"&& x!="*" && x!="/")
tmp.push(stoi(x));//stoi function take string Converted to an integer
else{
if(x=="+"){
b=tmp.top();
tmp.pop();
a=tmp.top();
tmp.pop();
c=a+b;
tmp.push(c);
}
else if(x=="-"){
// Note the order in which the elements are calculated
b=tmp.top();
tmp.pop();
a=tmp.top();
tmp.pop();
c=a-b;
tmp.push(c);
}
else if(x=="*"){
b=tmp.top();
tmp.pop();
a=tmp.top();
tmp.pop();
c=a*b;
tmp.push(c);
}
else{
// Divide Note the order in which the elements are calculated
b=tmp.top();
tmp.pop();
a=tmp.top();
tmp.pop();
c=a/b;
tmp.push(c);
}
}
}
int ans=tmp.top();
tmp.pop();
return ans;
}
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617011633.html
边栏推荐
- 治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
- 9.Life, the Universe, and Everything
- Collections multiple parameter sorting
- Algèbre linéaire chapitre 1 - déterminants
- PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
- C language file operation
- MySQL advanced query
- Graphic numpy array matrix
- Doomsday (simple computational geometry)
- How to grow at work
猜你喜欢

Generate excel template (drop-down selection, multi-level linkage)
![How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation](/img/33/780b80693f70112eebc10941f7c134.png)
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation

Advanced operation of idea debug
Best practices for MySQL storage time

自動控制(韓敏版)

Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)

Guaba and Computational Geometry

On traversal of binary tree

JDBC connection database
![对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning](/img/21/4bc94fe29f416c936436c04fc16fa8.png)
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
随机推荐
Sakura substring thinking
Customized communication between threads (reentrantlock)
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
In depth understanding of the relationship between dncblevel and noise denoising in the paper
Mysql database foundation
Linear algebra Chapter 2 - matrices and their operations
治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
Calculation (enter the calculation formula to get the result)
4. Print form
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
Kalman filter and inertial integrated navigation
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
线性代数第一章-行列式
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
線性代數第二章-矩陣及其運算
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
Use Matplotlib. In Jupiter notebook Pyplot server hangs up and crashes
Troubleshooting of data deleted and reappeared problems