leetcode——Evaluate Reverse Polish Notation 求算式值(AC)
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
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> myStack;
int first,second,merge;
for(int i=0; i<tokens.size(); i++)
{
if(tokens[i]=="+" || tokens[i]=="-" || tokens[i]=="*" || tokens[i]=="/")
{
first = myStack.top();
myStack.pop();
second = myStack.top();
myStack.pop();
if(tokens[i] == "+")
merge = first + second;
else if(tokens[i] == "-")
merge = second - first;
else if(tokens[i] == "*")
merge = first * second;
else if(tokens[i] == "/")
merge = second / first;
}
else
{
merge = atoi(tokens[i].c_str());
}
myStack.push(merge);
}
return myStack.top();
}
};
leetcode——Evaluate Reverse Polish Notation 求算式值(AC)的更多相关文章
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [leetcode]Evaluate Reverse Polish Notation @ Python
原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation stack 栈
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation [2]
题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
随机推荐
- MSA2312 enclosure 闪断后
故障描述:由于电源原因,导致整个扩展柜闪断,硬盘全部为leftover状态. 存储划分配置:之前满配的一套MSA2312,划分为4个vd,后面两个vd无影响,前面2个VD都是一半在1号柜子,一半在2号 ...
- libuv 与 libev 的对比
libuv 与 libev 的对比 libuv 与 libev 的对比 05 January 2013 libuv 和 libev ,两个名字相当相近的 I/O Library,最近有幸用两个 Lib ...
- 【机器学习实验】学习Python来分类现实世界的数据
引入 一个机器能够依据照片来辨别鲜花的品种吗?在机器学习角度,这事实上是一个分类问题.即机器依据不同品种鲜花的数据进行学习.使其能够对未标记的測试图片数据进行分类. 这一小节.我们还是从scikit- ...
- Python 实现的下载op海贼王网的图片(网络爬虫)
没得事就爬一下我喜欢的海贼王上的图片 须要在d盘下建立一个imgcache目录 # -*- coding: utf-8 -*- import urllib import urllib2 import ...
- 2014 Multi-University Training Contest 1 - J Rating
题目链接: pid=4870">http://acm.hdu.edu.cn/showproblem.php?pid=4870 题目大意: 题意:一个人注冊两个账号,初始rating都是 ...
- atitit.团队建设--要不要招技术储备人才的问题
atitit.团队建设--要不要招技术储备人才的问题 1. 人才的储备和招聘. 1 1.1. 模式1. 养兵千日,用兵一时 1 1.2. 模式2,暂时抱佛脚,也不多招一个人 1 ...
- windows服务的创建、安装和调试
1.创建 windows服务 项目 文件 -> 新建项目 -> 已安装的模板 -> Visual C# -> windows ,在右侧窗口选择"windows 服 ...
- SPARK如何使用AKKA实现进程、节点通信
SPARK如何使用AKKA实现进程.节点通信 <深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 < ...
- AngularJs 基础(60分钟入门) (转)
AngularJs是一个不错的用于开发SPA应用(单页Web应用)的框架.单页Web应用(single page web application,SPA),就是只有一张Web页面的应用.浏览器一开始会 ...
- [iOS]iOS8可用的识别用户方式(idfa、UUID、idfv)
本文地址:http://blog.csdn.net/zhaoyabei/article/details/46682765 想要追踪.统计用户,自然离不开用户唯一标识符.这是每一个公司都面临的问题.在历 ...