当前位置:网站首页>[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
边栏推荐
猜你喜欢

Explain of MySQL optimization

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

Pytorch learning record (7): skills in processing data and training models

MySQL table constraints and table design

卡尔曼滤波与惯性组合导航

线性代数第三章-矩阵的初等变换与线性方程组

Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations

C language file operation

编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察

Implementation of displaying database pictures to browser tables based on thymeleaf
随机推荐
6.Reversal
The problem that the page will refresh automatically after clicking the submit button on the form is solved
程序設計訓練
Pytorch learning record (7): skills in processing data and training models
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
Pytoch -- data loading and processing
Automatic control (Han min version)
C3p0 database connection pool usage
Illustrate the significance of hashcode
Explain of MySQL optimization
Code neat way to learn
Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
lambda expressions
Linear algebra Chapter 1 - determinant
Exception handling: grab and throw model
RPC must know and know
Programming training
Understanding and use of tp50, tp90 and tp99
Usage scenario of copyonwritearraylist