[LintCode] 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.
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6LeetCode上的原题,请参见我之前的博客Evaluate Reverse Polish Notation。
解法一:
class Solution {
public:
    /**
     * @param tokens The Reverse Polish Notation
     * @return the value
     */
    int evalRPN(vector<string>& tokens) {
    stack<int> s;
        for (auto a : tokens) {
            if (a == "+" || a == "-" || a == "*" || a == "/") {
                if (s.size() < ) break;
                int t = s.top(); s.pop();
                int k = s.top(); s.pop();
                if (a == "+") k += t;
                else if (a == "-") k -= t;
                else if (a == "*") k *= t;
                else if (a == "/") k /= t;
                s.push(k);
            } else {
                s.push(stoi(a));
            }
        }
        return s.top();
    }
};
解法二:
class Solution {
public:
    /**
     * @param tokens The Reverse Polish Notation
     * @return the value
     */
    int evalRPN(vector<string>& tokens) {
        int op = tokens.size() - ;
        return helper(tokens, op);
    }
    int helper(vector<string>& tokens, int& op) {
        string s = tokens[op];
        if (s == "+" || s == "-" || s == "*" || s == "/") {
            int v2 = helper(tokens, --op);
            int v1 = helper(tokens, --op);
            if (s == "+") return v1 + v2;
            else if (s == "-") return v1 - v2;
            else if (s == "*") return v1 * v2;
            else return v1 / v2;
        } else {
            return stoi(s);
        }
    }
};
[LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式的更多相关文章
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
		Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ... 
- [LeetCode] 150. 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 OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
		Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ... 
- LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
		题目: Evaluate Reverse Polish Notation Evaluatethe value of an arithmetic expression in Reverse Polish ... 
- Evaluate Reverse Polish Notation(堆栈)
		Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ... 
- 150. 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 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ... 
- 【leetcode】Evaluate Reverse Polish Notation
		Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ... 
随机推荐
- linux文本模式下使用PPPOE拨号ADSL上网的方法
			转自:http://www.myzhenai.com.cn/post/945.html 转载请注明出处:http://www.myzhenai.com/thread-15431-1-1.html ht ... 
- 利用Visual GDB在Visual Studio中进行Android开发
			转载请注明http://www.cnblogs.com/adong7639/p/4119467.html 无意中发现了Visual GDB这个工具,可以再Visual Studio中进行Android ... 
- HDU 5145 NPY and girls 莫队+逆元
			NPY and girls Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more! ... 
- 十天来学习java的心得体会
			有关学习java是几天来的心得体会: 十天学习java遇到很多问题,每个问题都是经过反复的看书本以及上网查找资料来解决的,发现这一点真的需要自己来而不是去遇到什么问题就去依靠他人(师兄.同学).在其中 ... 
- ios本地推送
			#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate //无论程序在 ... 
- CE搜索内存数据的原理
			最近发现有朋友在玩游戏时, 使用一款工具来修改游戏的部分数据,作弊的效果, 也就是CE(Cheat Engine),这款工具是 delphi 编写的, 于是好奇, 然后瞬间想到API OpenPr ... 
- loadrunner取出字符串的后面几位
			Action() { char *phonenum; int k=1; phonenum=lr_eval_string("{phoneNum}");//参数化获取 ... 
- js的一些小笔记,(不定期更新)
			2个$的用法$本身并无特定意义,它表示什么意思要看是如何定义的,如果没有定义就便是两个$,可能是变量名的开始.一般是一个函数,用来代替document.getElementByIdfunction $ ... 
- jQuery事件和JavaScript事件
			1.JavaScript事件: 属性 当以下情况发生时,出现此事件 FF N IE onabort 图像加载被中断 1 3 4 onblur 元素失去焦点 1 2 3 onchange 用户改变域的内 ... 
- [技术学习]js继承
			今天又看了一遍js的面向对象方面的知识,重点看了继承相关内容,已经记不得看了第几次这个内容,终于觉得自己好像懂了,特记录下来过程. js面向对象继承分为两大类,主要分为对象继承和非对象继承(拷贝继承) ... 
