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 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 地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
计算波兰后缀表达式的值,具体做法是利用一个栈,如果遇到数字则把数字进栈,如果遇到操作符就把栈中的数字出栈进行运算(二元操作符出栈两个数字,一元操作符出栈一个数字),最后的结果存在栈中。代码:
class Solution {
public:
int evalRPN(vector<string> &tokens) {
int lop, rop;
vector<string>::const_iterator cIter = tokens.begin();
stack<int> s;
for(; cIter != tokens.end(); ++cIter){
if ((*cIter).size() == && !isdigit((*cIter)[])){
char op = (*cIter)[];
rop = s.top();
s.pop();
lop = s.top();
s.pop();
if ('+' == op){
s.push(lop + rop);
} else if ('-' == op){
s.push(lop - rop);
} else if ('*' == op){
s.push(lop * rop);
} else{
s.push(lop / rop);
}
} else {
s.push(atoi((*cIter).c_str()));
}
}
return s.top();
}
};
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation的更多相关文章
- LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode150:Evaluate Reverse Polish Notation
题目: 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 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- HDU——1062Text Reverse(水题string::find系列+reverse)
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
随机推荐
- Bone.io是一个轻量级的框架构建高性能实时单页HTML5应用程序
Bone.io允许你使用HTML5 WebSockets构建实时应用程序,提供“热”数据到浏览器.这使您可以轻松地构建丰富的,高度响应的用户界面. 项目主页:http://www.open-open. ...
- ISO/IEC 14496 文档内容简介, MPEG标准
ISO/IEC 14496是MPEG专家组制定的MPEG-4标准于1998年10月公布第1版,1999年1月成为国际标准,1999年12月公布了第2版,2000年初成为国际标准. 全文分为21个部分: ...
- 记录一次Android交叉编译ffmpeg排查错误
Android版本手机直播引擎中,引用了libvlc开源库.项目接过来,发现编译脚本中使用了很多用户名下的绝对路径.项目相关人离职,导致这个脚本实际上已经废掉.而且不知道相关路径下有没有其他文件和第三 ...
- 批量ping主机脚本
#! /bin/bash for i in `cat test.list`do host=`echo $i|awk -F"," '{print $1}'` app_IP=` ...
- homework03
代码实现真的是大问题……在第二次作业还没有真正实现的情况下只能写这么一篇博客来整理一下从各位大神那里看到的东西. 两个弱菜加起来同样是弱菜,所以我和我的小伙伴的配合就是悲剧的聚合. 首先,大家都说C# ...
- 现代程序设计 homework-08
现代程序设计 homework-08 第八次作业. 理解C++变量的作用域和生命周期 作用域就是一个变量可以被引用的范围,如:全局作用域.文件作用域.局部作用域:而生命周期就是这个变量可以被引用的时间 ...
- Java缓存学习之一:缓存
一.缓存 1.什么是缓存? 缓存是硬件,是CPU中的组件,CPU存取数据的速度非常的快,一秒钟能够存取.处理十亿条指令和数据(术语:CPU主频1G),而内存就慢很多,快的内存能够达到几十兆就不错了,可 ...
- Java设计模式系列之中介者模式
中介者模式(Mediator)的定义 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式(Mediator)的适 ...
- DelphiXE8新建AVD
相关资料: 1.http://jingyan.baidu.com/article/ea24bc398576b3da62b33107.html
- CodeForces 706C Hard problem (水DP)
题意:对于给定的n个字符串,可以花费a[i] 将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...