当前位置:网站首页>[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
边栏推荐
- Formation à la programmation
- 线性代数第三章-矩阵的初等变换与线性方程组
- 图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
- JDBC connection database
- 7.Domino piling
- Mysql database foundation
- Programming training
- Detection technology and principle
- Exception handling: grab and throw model
- Framework analysis 2 Source code - login authentication
猜你喜欢
Guaba and Computational Geometry
A sharp tool to improve work efficiency
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
Substring Inversion (Easy Version)
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Understanding and installing MySQL
MySQL advanced query
Algèbre linéaire chapitre 1 - déterminants
随机推荐
Collection and map thread safety problem solving
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
String notes
线性代数第三章-矩阵的初等变换与线性方程组
List segmentation best practices
Collections multiple parameter sorting
线代第四章-向量组的线性相关
SQL optimization best practices
9.Life, the Universe, and Everything
3. Continuous integer
卡尔曼滤波与惯性组合导航
治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
Plane semi intersecting plate
Chapter 4 of line generation - linear correlation of vector systems
Sakura substring thinking
Mysql database foundation
Delete and truncate
Gaussian processes of sklearn
檢測技術與原理