当前位置:网站首页>逆波兰表达式求值
逆波兰表达式求值
2022-08-03 22:55:00 【老鱼37】
思路:
class Solution {
public:
int evalRPN(vector<string>& tokens) {
//创建一个栈
stack<int>v;
for(string &ch:tokens)
{
if(ch=="+"||ch=="-"||ch=="/"||ch=="*")
{
//弹出栈中两个元素,进行运算
int b=v.top(); v.pop();
int a=v.top(); v.pop();
if(ch=="+") v.push(a+b);
else if(ch=="-") v.push(a-b);
else if(ch=="*") v.push(a*b);
else{
v.push(a/b);
}
}
else{
//数字入栈
v.push(stoi(ch));//将字符转换成数字 方便运算
}
}
return v.top();
}
};
解法二:
class Solution {
public:
int evalRPN(vector<string>& tokens) {
//也可以使用case语句
stack<int>v;
for(int i=0;i<tokens.size();i++)
{
string &str=tokens[i];
if(!("+" == str||" - " == str||" * " == str || "/ " == str))
{
v.push(atoi(str.c_str()));//int num = atoi(str.c_str());
}
else
{
int b=v.top(); v.pop();
int a=v.top(); v.pop();
switch(str[0])
{
case '+':
v.push(a+b);
break;
case '-':
v.push(a-b);
break;
case '*':
v.push(a*b);
break;
case '/':
v.push(a/b);
break;
}
}
}
return v.top();
}
};
扩展:
aoti文章转载处:aoti函数详解
如有错误,多多指教
边栏推荐
- Cloud platform construction solutions
- Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
- [N1CTF 2018] eating_cms
- What is memoization and what is it good for?
- BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
- ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
- LabVIEW代码生成错误 61056
- Makefile
- 2019年10月SQL注入的两倍
- encapsulation, package, access modifier, static variable
猜你喜欢
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
Walk the Maze BFS
[N1CTF 2018] eating_cms
软测人每个阶段的薪资待遇,快来康康你能拿多少?
encapsulation, package, access modifier, static variable
Pytest learn-setup/teardown
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
ML之yellowbrick:基于titanic泰坦尼克是否获救二分类预测数据集利用yellowbrick对LoR逻辑回归模型实现可解释性(阈值图)案例
2022-08-03 Oracle executes slow SQL-Q17 comparison
Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan
随机推荐
golang写的存储引擎,基于b+树,mmap
剑指offer第22题-链表中倒数第K个节点
Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
完全二叉树问题
Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
Why do we need callbacks
Golang Chapter 2: Program Structure
Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
2022-08-02 mysql/stonedb慢SQL-Q18-内存使用暴涨分析
Testng监听器
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
LabVIEW code generation error 61056
Conditional Statements for Shell Programming
log4j-slf4j-impl cannot be present with log4j-to-slf4j
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
complete binary tree problem
UVa 10003 - Cutting Sticks (White Book, Interval DP)
ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
Binary search tree to solve the fallen leaves problem