11. 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
说明:使用一个数字栈即可。(也可用数组链表模拟栈)(注意负数)
inline int newPop(stack<int> &digits) {
int v = digits.top();
digits.pop();
return v;
}
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> digits;
for(size_t id = 0; id < tokens.size(); ++id) {
if((tokens[id][0] == '-' && tokens[id].size() > 1 ) ||
(tokens[id][0] >= '0' && tokens[id][0] <= '9')) digits.push(atoi(tokens[id].c_str()));
else
{
int v;
switch(tokens[id][0]){
case '+': {
digits.push(newPop(digits)+newPop(digits));
break;
}
case '-': {
v = newPop(digits);
digits.push(newPop(digits)-v);
break;
}
case '*': {
digits.push(newPop(digits)*newPop(digits));
break;
}
case '/': {
v = newPop(digits);
digits.push(newPop(digits)/v);
break;
}
}
}
}
return digits.top();
}
};
11. Evaluate Reverse Polish Notation的更多相关文章
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 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 Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- 关闭缓存和mmu(转)
当设置完时钟分频以后,uboot就会执行cpu_init_crit汇编函数,这个函数的主要作用就是关闭缓存和mmu,然后调用lowlevel_init函数进行系统总线的初始化. 为什么启动的时候,需要 ...
- ubuntu安装无线驱动
一.无线驱动的安装. 首先查看自己的网卡类型:lspci -vnn | grep 14e4 查看内核版本: -uname -r 去www.broadcom.com/support/802.11/lin ...
- Jmeter—4 添加断言 判断响应数据是否符合预期
发出请求之后,通过添加断言可以判断响应数据是否是我们的预期结果. 1 在Jmeter中发送一个登录的http请求(参数故意输入错误).结果肯定是登陆失败啦. 但结果树中http请求的图标显示‘绿色’表 ...
- 游戏贴图中常用术语《DC》的理解
什么是DC呢? 在GDI中,DC(Device Context)是一个非常重要的概念. 有的书中,将DC翻译为设备描述表,也有的书中翻译为设备上下文. 但是这些翻译,无法在我们的头脑里有强烈的冲击,无 ...
- 在VS下使用 GitFlow管理项目开发
在VS下使用 GitFlow管理项目开发 1.右键将你的解决方案添加到源代码管理,如果你的VS没有安装git,会提示安装,安装完成之后,在团队资源管理可以看到如下界面 (图一) 2.安装gitflow ...
- sql datetime操作
Sql Server中的日期与时间函数 SQL中的时间函数非常有用,特别是在我们进行初始赋值.复杂查询的时候,就显得特别方便. 1.获得系统当前时间 select getdate() 2.DateN ...
- 用php完成数据库的增删改查
<?php//一.连接数据库$con = mysql_connect("localhost","root","");//二.验证是否连 ...
- wndows程序设计之书籍知识与代码摘录-封装一个类似printf的messagebox
//----------------------------------------- //本程序展示了如何实现MessageBoxPrintf函数 //本函数能像printf那样格式化输出 //摘录 ...
- 嵌入式linux应用开发完全手册学习笔记一
2015.3.25星期三 晴 有两个星期没写学习日记了,找个时间把这段时间做的电子词典和ARM小项目总结一下. 下面的知识点总结,U-BOOT:参考PDF文档:嵌入式linux应用开发完全手册 当虚拟 ...
- Apache2.2与php5.17 mysql配置
php5.217应该用线程安全搬,不然各种无语的Apache打不开,PHPInfo没有Mysql的信息,记得把php.ini放入系统盘Windows目录下,Win764位的libmysql.dll也放 ...