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 ...
随机推荐
- HDU4405(期望dp)
标准期望套路,很水.读题看好是到n就可以停止了. ; int n, m; db dp[maxn]; map<int, int> mp; int main() { while (~scanf ...
- Hive_Hive的数据模型_内部表
Hive的数据模型_内部表 - 与数据库中的Table在概念上是类似.- 每一个Table在Hive中都有一个相应的目录存储数据.- 所有的Table数据(不包括External Table)都保存在 ...
- vue.js 中如何解除绑定事件
我们项目中有一个点赞需求,只允许点击一次赞,再次点击则取消赞, 为了防止用户多次连续点击,在点赞后需要解绑事件,成功调取API后,才可再次点击取消赞. 目前用的方法是加入一个flag控制点击事件可否点 ...
- Super Mario(线段树离线区间k值)
以前见过这题,没做出来,知道是离线处理,这次仔细想了下, 首先把出现的高度都map离散化一下,以离散化出来的数目g建树,把每个位置都开俩个vector,一个存以这个位置为L的询问,一个存以这个位置为R ...
- codeforces1025
hackforces + fstforces A 很明显当有一个字母出现次数>1时即合法 $n = 1$的情况需要特判 #include<cstdio> #include<ve ...
- 2017 清北学堂 Day 6终极考试报告
预计分数: 100+70+70 = 240 实际假分数 : 40+80+70= 190 in cena(好吧不得不承认这个分数,,,,,,=.=) 实际真分数 : 100+80+100 = 280 ...
- Android.mk模板
此文列出Android.mk的常用模板(部分内容源于多篇他人博客,这里不具体指出),如有错漏,还请在评论中指出,后期持续更新 #链接第三方动态库,在和部分android源码的编译中验证不过 LOC ...
- Actionbar Demo
源码下载:http://download.csdn.net/detail/bx276626237/8874119
- 查询sqlserver数据库,表占用数据大小
if exists(select 1 from tempdb..sysobjects where id=object_id('tempdb..#tabName') and xtype='u')dro ...
- Hdoj—1789
//大意理解 先排序 最早交的里面选最大值 扫描完了加没写的 排序后 应该是早交的和扣分多的在前 用结构体吧/*#include<stdio.h>#include<stdio.h&g ...