LeetCode(150) Evaluate Reverse Polish Notation
题目
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
分析
本题考查的是栈的应用,计算后缀表达式的值。
参考数据结构,栈章节。
AC代码
class Solution {
public:
int evalRPN(vector<string>& tokens) {
if (tokens.empty())
return 0;
//存储运算数
stack<int> s;
//tokens容量
int size = tokens.size();
for (int i = 0; i < size; ++i)
{
if (!isOper(tokens[i]))
{
s.push(strToInt(tokens[i]));
}
else{
char op = tokens[i][0];
switch (op)
{
int op1, op2;
case '+':
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(op2 + op1);
break;
case '-':
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(op2 - op1);
break;
case '*':
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(op2 * op1);
break;
case '/':
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(op2 / op1);
break;
default:
break;
}//switch
}//else
}//for
return s.top();
}
//判断是否为运算符
bool isOper(string &str)
{
if (str.size() > 1)
return false;
if (str[0] == '+' || str[0] == '-' || str[0] == '*' || str[0] == '/')
return true;
return false;
}
//将字符串转换为整数
int strToInt(string &str)
{
if (str.empty())
return 0;
// 求字符串长度
int size = str.size();
int flag = 1, pos = 0, sum = 0, multi = 1;
if (str[0] == '-')
{
flag = -1;
pos = 1;
}
for (int i = size - 1; i >= pos; --i)
{
sum += (str[i] - '0') * multi;
multi *= 10;
}
return flag * sum;
}
};
LeetCode(150) Evaluate Reverse Polish Notation的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
随机推荐
- light OJ 1282 - Leading and Trailing 数学 || double技巧
http://lightoj.com/volume_showproblem.php?problem=1282 #include <cstdio> #include <cstdlib& ...
- D. Anton and Chess 模拟题 + 读题
http://codeforces.com/contest/734/problem/D 一开始的时候看不懂题目,以为象是中国象棋那样走,然后看不懂样例. 原来是走对角线的,长知识了. 所以我们就知道, ...
- auth_basic 认证
shell > yum -y install httpd-tools # 安装 htpasswd 工具 shell > cd /usr/local/nginx-/conf shell &g ...
- [转]AngularJS移动开发中的坑汇总
使用AngualrJs开发移动App已经快半年了,逐渐积累了很多AngularJS的问题,特别是对于用惯了Jquery的开发者,转到AngularJS还是需要克服很多问题的.不像Jquery那样侧重D ...
- [转]为ReportViewer导出的PDF文档加上水印
接到一個頗富挑戰性的需求,Reporting Service或RDLC報表可匯出成Excel.PDF等檔案格式,對一般麻瓜型使用者而言,PDF唯讀,Excel則可修改,業務單位希望在拿到報表紙本時加以 ...
- JS实现的图片预览功能
之前的博文有实现过图片上传预览,但那种方法是预览时就将图片上传,会产生很大的浪费空间.找到了之前有人写的用JS实现的图片预览,就说用js将上传的图片显示,上传代码在之前的博文中有写到. 以下是实现的代 ...
- PeopleSoft FSCM Production Support 案例分析
PeopleSoft FSCM Production Support 案例分析 2010年的时候曾建言博客园开辟Oracle ERP模块供大家交流,博客园如约开辟Oracle ERP 模块,而我后来却 ...
- 打开一个本地apk进行安装
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); File file = new File(Environment ...
- Azure powershell 获取 vmSize 可用列表的命令
1.使用 Add-AzureAccount -Environment azurechinacloud 登录到订阅 2.选择默认的订阅 Select-AzureSubscription -Subscri ...
- linux 删除文件后空间没有释放的解决办法
清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [root@ticketb ~]# df -hFilesystem ...