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. POJ 2226 最小点覆盖(经典建图)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8881   Accepted: 3300 Desc ...

  2. SharePoint 2013 开发——其他社交功能

    博客地址:http://blog.csdn.net/FoxDave 上一篇讲了如何获取用户配置文件的相关属性,它属于SharePoint 2013社交功能的一个小的构成部分.社交功能是SharePoi ...

  3. MAC机中安装ruby环境--转载

    http://www.cnblogs.com/pingfan1990/p/4957162.html

  4. CSS3-Media Query 基础

    一.常见的属性: device-width , device-height 屏幕宽高 width , height 渲染窗口宽高 orientation 设备方向 resolution 设备分辨率 二 ...

  5. InputStream流保存成图片文件

    public void saveBit(InputStream inStream) throws IOException{ ByteArrayOutputStream outStream = new ...

  6. Android使用SharedPreference存储数据

    SharedPreference存储数据和文件存储更加方便的一点是可以按照一定的数据类型进行存储,同时取数据时也能够获取到相应的数据类型.它是按照map的方式来存储和读取数据的. MainActivi ...

  7. MVC中的自定义控件

    MVC中的控件都是HtmlHelper的扩展方法(不了解扩展方?法请阅读扩展方法),比如@Html.ActionLink,F12可以看到它是这样写的: public static MvcHtmlStr ...

  8. ZeroMQ - 三种模型的python实现

    ZeroMQ是一个消息队列网络库,实现网络常用技术封装.在C/S中实现了三种模式,这段时间用python简单实现了一下,感觉python虽然灵活.但是数据处理不如C++自由灵活. 1.Request- ...

  9. Multiply game_线段树

    Problem Description Tired of playing computer games, alpc23 is planning to play a game on numbers. B ...

  10. 观看github前100开源项目的读后感

    文章来自:http://www.oschina.net/news/61416/github-top-100-objective-c-projects?from=20150412 ReactiveCoc ...