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 ...
随机推荐
- JavaScript中的eval()函数
和其他很多解释性语言一样,JavaScript同样可以解释运行由JavaScript源代码组成的字符串,并产生一个值.JavaScript通过全局函数eval()来完成这个工作. eval(“1+2” ...
- Node.js 全局对象
JavaScript 中有一个特殊的对象,称为全局对象(Global Object),它及其所有属性都可 以在程序的任何地方访问,即全局变量. 在浏览器JavaScript 中,通常window 是全 ...
- RSS
RSS的基本概念 什么是RSS,RSS是在线共享内容的一种简易方式(也叫聚合内容,Really Simple Syndication).网站提供RSS输出有利于让用户获取网站内容的最新更新.用户可以使 ...
- MySQL主机127.0.0.1与localhost区别总结
1. mysql -h 127.0.0.1 的时候,使用TCP/IP连接, mysql server 认为该连接来自于127.0.0.1或者是"localhost.localdomain&q ...
- 11-10 CC150第一章
题目: 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can n ...
- windows核心编程---第六章 线程的调度
每个线程都有一个CONTEXT结构,保存在线程内核对象中.大约每隔20ms windows就会查看所有当前存在的线程内核对象.并在可调度的线程内核对象中选择一个,将其保存在CONTEXT结构的值载入c ...
- angularjs中展示富文本编辑器文本,向DOM中插入元素
前几天在用textangular富文本编辑器插件时,将存储的文本及格式存储到数据库中,但是从后台接口中再向angular页面插入时却不能执行,即在Angular中操作DOM没有实现,后来查看了一下,操 ...
- SPOJ BALNUM
一开始题看错了...dp[pos][sets][viss],其中sets表示出现次数,viss表示出现没有. #include<iostream> #include<cstdio&g ...
- 相同vlan之间的相互访问
- Oracle 12c SYSAUX表空间不足处理-清理audsys.cli_swp$a9b5f52c$1$1表
今天在检查一台测试环境的表空间时,发现SYSAUX的使用率已经达到99.91% TABLESPACE_NAME FILES Freesize(MB) Usedsize(MB) Filesize(MB) ...