当前位置:网站首页>[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
边栏推荐
- Customized communication between threads (reentrantlock)
- Use Matplotlib. In Jupiter notebook Pyplot server hangs up and crashes
- A general U-shaped transformer for image restoration
- container
- 深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
- Doomsday (simple computational geometry)
- 檢測技術與原理
- Pytoch -- data loading and processing
- 卡尔曼滤波与惯性组合导航
- Mysql database foundation
猜你喜欢

Illustrate the significance of hashcode

20 excellent plug-ins recommended by idea
![如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation](/img/33/780b80693f70112eebc10941f7c134.png)
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation

String notes

Graphic numpy array matrix

电机与拖动(戚金清版)学习整理

Addition, deletion, modification and query of MySQL table

图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration

Linear algebra Chapter 1 - determinant

线性代数第一章-行列式
随机推荐
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
Fundamentals of digital image processing (Gonzalez) I
[leetcode 67] sum of two binary numbers
Exception handling: grab and throw model
Substring Inversion (Easy Version)
2. Average length of words
[leetcode169] most elements
Example of ticket selling with reentrant lock
JDBC operation transaction
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
Sakura substring thinking
Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package
Formation à la programmation
Automatic control (Han min version)
自动控制(韩敏版)
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
Pytorch learning record (7): skills in processing data and training models
JSP syntax and JSTL tag
深度学习基础——简单了解meta learning(来自李宏毅课程笔记)