Leetcode 之Evaluate Reverse Polish Notation(41)

很简单的一道题,定义一个栈保留操作数,遇操作符则弹出运算即可。
bool isOperator(string &op)
{
//注意用法
return op.size() == && string("+-*/").find(op) != string::npos;
}
int evalRPN(vector<string> &tokens)
{
stack<string> s;
for (auto token : tokens)
{
if (!isOperator(token))
{
//如果是操作数,则入栈
s.push(token);
}
else
{
//如果是操作符,则弹出操作数进行运算
int y = stoi(s.top());
s.pop();
int x = stoi(s.top());
s.pop();
if (token == "+")x += y;
if (token == "-")x -= y;
if (token == "*")x *= y;
if (token == "/")x /= y;
s.push(to_string(x));
}
}
return stoi(s.top());
}
Leetcode 之Evaluate Reverse Polish Notation(41)的更多相关文章
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
随机推荐
- POI 2018.10.27
[POI2015]LOG 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进 ...
- 关于AVPlayerItem对象的属性duration返回播放总时长的坑
最近在使用AVPlayer播放网络流媒体,发现一个坑: 就是playerItem.duration有可能不返回该网络多媒体资源的播放总时间长度,而是返回了一个奇怪的数据:nan, 因为我通过CMTim ...
- ios开关按钮
.al-toggle-button{ appearance: none; -webkit-appearance: none; position: relative; width: 52px; heig ...
- [技巧篇]16.MyEclipse2014安装SVN插件,在线安装
这两天想做点东西,但是现在流行的是git,但是军哥的项目是托管到阿里的svn代码当中,所以没有办法,还需要弄SVN,这里不将什么安装SVN等东西 我要求的就是快速入门而已,仅此而已,我安装成功,过程很 ...
- Java设计模式の工厂模式
-------------------------------------------------------- 目录: 一.序言 二.简单工厂模式 三.工厂方法模式 四.简单工厂和工厂方法模式的比 ...
- iOS 点击cell上的按钮获取行数
-(void)btnClick:(UIButton *)button{ UITableViewCell *cell = (UITableViewCell *)[[button superview] s ...
- .net core Fundamentals
• Application Startup 應用程序啟動 • Middleware 中間件 • Working with Static Files 靜態文件 • Routing 路由 • URL Re ...
- Android通知栏介绍与适配总结
由于历史原因,Android在发布之初对通知栏Notification的设计相当简单,而如今面对各式各样的通知栏玩法,谷歌也不得不对其进行更新迭代调整,增加新功能的同时,也在不断地改变样式,试图迎合更 ...
- 打印菱形(c语言)
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { // 定 ...
- Codeforces Round #482 (Div. 2) B题
题目链接:http://codeforces.com/contest/979/problem/B B. Treasure Hunt time limit per test1 second memory ...