[leetcode]evaluate-reverse-polish-notation

5人阅读 评论(0) 收藏 举报
分类:

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
class Solution {
public:
    int evalRPN(vector<string> &tokens) {
        if(tokens.size() == 0)
            return 0;
        stack<int> st;
        for(int i = 0; i < tokens.size(); ++i)
        {
            string s = tokens[i];
            if(s == "+" || s == "-" || s == "*" || s == "/")
            {
                if(st.size() < 2)
                    return 0;
                int num2 = st.top(); st.pop();
                int num1 = st.top(); st.pop();
                int result = 0;

                if(s == "+")
                    result = num1 + num2;
                else if(s == "-")
                    result = num1 - num2;
                else if(s == "*")
                    result = num1 * num2;
                else if(s == "/")
                    result = num1 / num2;
                st.push(result);
            }
            else
            {
                st.push(atoi(s.c_str()));
            }  
        }
        return st.top();
    }
};
查看评论

Evaluate Reverse Polish Notation -- LeetCode

原题链接: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/  这道题是逆波兰式的求解,不了解逆波兰式的朋友可以参考一...
  • linhuanmars
  • linhuanmars
  • 2014-03-13 21:56:02
  • 5440

LeetCode 150 — Evaluate Reverse Polish Notation(C++ Java Python)

题目:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ Evaluate the value of an arithm...
  • dragon_dream
  • dragon_dream
  • 2014-03-01 15:34:39
  • 2531

[LeetCode] evaluate-reverse-polish-notation

evaluate-reverse-polish-notation题目描述Evaluate the value of an arithmetic expression in Reverse Polish...
  • u014293306
  • u014293306
  • 2016-08-12 21:16:02
  • 120

evaluate-reverse-polish-notation(Leetcode)

将操作数放在一个栈中,每遇到一个操作符从栈中弹出两个数字
  • sxd8700
  • sxd8700
  • 2016-05-29 22:32:51
  • 177

Leetcode:evaluate-reverse-polish-notation

题目描述: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid opera...
  • weixin_37933986
  • weixin_37933986
  • 2017-05-22 22:47:24
  • 112

leetcode evaluate-reverse-polish-notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*...
  • Fly_TheWind
  • Fly_TheWind
  • 2016-05-30 18:56:10
  • 198

【LeetCode】Evaluate Reverse Polish Notation 解题报告

【题意】 逆波兰表达式,又叫后缀表达式。 例如: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]...
  • ljiabin
  • ljiabin
  • 2014-08-28 20:57:36
  • 1799

Leetcode在线编程evaluate-reverse-polish-notation

Leetcode在线编程minimum-depth-of-binary-tree 后缀表达式,栈
  • zero_witty
  • zero_witty
  • 2016-12-03 01:00:42
  • 175

【evaluate-reverse-polish-notation】

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*...
  • lplp90908
  • lplp90908
  • 2017-09-01 15:45:41
  • 70

刷LeetCode刷LeetCode刷LeetCode刷LeetCode

  • 2018年01月09日 15:07
  • 1.55MB
  • 下载
    个人资料
    持之以恒
    等级:
    访问量: 5万+
    积分: 3159
    排名: 1万+
    最新评论