zoukankan      html  css  js  c++  java
  • evaluate-reverse-polist-notation leetcode C++

    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

    C++

    class Solution {
    public:
        int evalRPN(vector<string> &tokens){
            int len = tokens.size();
            stack<int> S;
            for (int i = 0; i< len; i++){
                if ("+" == tokens[i] || "-" == tokens[i] || tokens[i] == "*" || tokens[i] == "/"){
                    int arg2 = S.top(); S.pop();
                    int arg1 = S.top(); S.pop();
                    S.push(runOperator(arg1,arg2,tokens[i][0]));
                }else
                    S.push(stoi(tokens[i]));
            }
            return S.top();
        }
        int runOperator(int arg1,int arg2,char optor){
            if('+' == optor) return arg1 + arg2;
            else if('-' == optor) return arg1 - arg2;
            else if('*' == optor) return arg1 * arg2;
            else return arg1 / arg2;
        }
    };
  • 相关阅读:
    Hashmap实现原理
    策略模式
    Google Drive ubuntu
    numix Docky
    Google Drive 和 Dropbox 同步同一个文件夹目录
    sublime text 2
    matlab cell
    liteide
    taglist and nerdtree
    codeblocks
  • 原文地址:https://www.cnblogs.com/vercont/p/10210284.html
Copyright © 2011-2022 走看看