当前位置:网站首页>逆波兰表达式求值
逆波兰表达式求值
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函数详解
如有错误,多多指教
边栏推荐
- 易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
- [N1CTF 2018] eating_cms
- Create function report error, prompting DECLARE definition syntax problem
- 网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)
- HCIP BGP lab report
- 七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
- What is the difference between the generator version and the viewer version?
- 2022-08-03 oracle执行慢SQL-Q17对比
- 完全二叉树问题
- Take an example of a web worker
猜你喜欢

What is the difference between the generator version and the viewer version?

OPC UA 与IEC61499 深度融合(1)

Software testing is seriously involution, how to improve your competitiveness?

Adobe是什么?
![navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication](/img/09/a579c60e07cdc145175e72673409f7.png)
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication

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

for loop exercises

【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve

3D 语义分割——2DPASS

重发布实验报告
随机推荐
Zilliz 2023 秋季校园招聘正式启动!
【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。
云计算国内外发展现状
易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
UVa 10003 - Cutting Sticks (White Book, Interval DP)
113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
Internet user account information management regulations come into effect today: must crack down on account trading and gray products
Pytest learn-setup/teardown
2019年10月SQL注入的两倍
Nine ways to teach you to read the file path in the resources directory
override learning (parent and child)
什么是memoization,它有什么用?
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
complete binary tree problem
亿流量大考(2):开发一套高容错分布式系统
The salary of soft testers at each stage, come to Kangkang, how much can you get?
Quickly build a website with static files
Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
How to write a database document management tool based on WPF (2)
End-to-End Lane Marker Detection via Row-wise Classification