当前位置:网站首页>逆波兰表达式求值
逆波兰表达式求值
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函数详解
如有错误,多多指教
边栏推荐
- UVa 437 - The Tower of Babylon (White Book)
- Conditional Statements for Shell Programming
- 2022-08-03 oracle执行慢SQL-Q17对比
- 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!
- Cloud platform construction solutions
- First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
- Golang第一章:入门
- Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
- 工作小计 QT打包
- P1996 约瑟夫问题
猜你喜欢

Canvas App中点击图标生成PDF并保存到Dataverse中

Walk the Maze BFS

node连接mysql数据库报错:Client does not support authentication protocol requested by server

V8中的快慢数组(附源码、图文更易理解)

Cloud platform construction solutions

CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply

网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)

软测人每个阶段的薪资待遇,快来康康你能拿多少?

.NET6之MiniAPI(十四):跨域CORS(上)

override learning (parent and child)
随机推荐
FinClip,助长智能电视更多想象空间
[RYU] rest_router.py source code analysis
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
noip preliminary round
Embedded systems: overview
举一个 web worker 的例子
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)
Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan
IELTS essay writing template
Nine ways to teach you to read the file path in the resources directory
Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
Cloud platform construction solutions
3D 语义分割——2DPASS
On the Qixi Festival of 2022, I will offer 7 exquisite confession codes, and at the same time teach you to quickly change the source code for your own use
【MySQL进阶】数据库与表的创建和管理
utlis thread pool
Binary search tree to solve the fallen leaves problem
Republish the lab report