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的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  2. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  3. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  4. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. 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 ...

  6. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  8. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  9. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. 什么是CPA, CPS, CPT?

    在互联网上或移动端进行产品推广时,经常听到很多术语,什么CPA,CPS,CPT等等.不知是怎么来的,今天网上搜一下术语,在这里做一下笔记. CPA(Cost Per Action) 每行动成本. CP ...

  2. css盒子模型层级3D图

    作为前端开发工程师,大家都应该知道盒子模型.下面用一张图来表达3D盒子模型的层级关系 大家可以看到background-color 在background-image的下一层.这个希望对大家有帮助

  3. HBase with MapReduce (SummaryToFile)

    上一篇文章是实现统计hbase单元值出现的个数,并将结果存放到hbase的表中,本文是将结果存放到hdfs上.其中的map实现与前文一直,连接:http://www.cnblogs.com/ljy20 ...

  4. DIY FSK RFID Reader

    This page describes the construction of an RFID reader using only an Arduino (Nano 3.0 was tested, b ...

  5. 网页设计、java、Andorid资源清单整理

    多学多练做笔记很重要. 赤裸裸的干货非鸡汤 网页设计的主要技术:    Html,    Js,    Css,    Ps       HTML/HTML5     网页的基础Html必须知道的.但 ...

  6. [GodLove]Wine93 Tarining Round #1

    比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44664#overview 题目来源: 2011 Asia Regional ...

  7. linux 远程管理

    启动linuxssh 服务: /etc/init.d/ssh 启动网络服务: service network restart linux远程登录配置过程: 首先在ubuntu下安装openssh-se ...

  8. jquery插件文件上传

    文件上传有很多jQuery插件,一般我最为常用的就是uploadify.js和ajaxfileupload.js,二者都是以file标签为依托,前者需要在页面初始化时就渲染插件,比较适合单纯的文件上传 ...

  9. 如何在一个网站或者一个页面规划JS

    规划主要分为两部分:1.JS的分层,2.Js的规划 1.JS的分层(功能) 1-1.底层的库 : jquery  1-2.组件(ui) : 比如拖拽等,模块之间没有必然的联系,可以重复利用  1-3. ...

  10. PCL常见编程问题

    1.如何获取pcd文件点云里点的格式,比如是pcl::PointXYZ还是pcl::PointXYZRGB等类型? #include <pcl/io/pcd_io.h> #include ...