zoukankan      html  css  js  c++  java
  • [leetcode]Evaluate Reverse Polish Notation

    简单模拟题

    class Solution {
    public:
        int parseInt(string& x){
            stringstream ss;
            int res = 0;
            ss << x;
            ss >> res;
            return res;
        }
        void op(char op , stack<int>& st){
            int num1 , num2;
            num1 = st.top() ; st.pop();
            num2 = st.top() ; st.pop();
            switch(op){
               case '+' : st.push(num2 + num1);break;
               case '-' : st.push(num2 - num1);break;
               case '*' : st.push(num2 * num1);break;
               case '/' : st.push(num2 / num1);break;
            }
        }
        bool isOp(char op){
            return op == '+' || op == '-' || op == '*' || op == '/';
        }
        int evalRPN(vector<string> &tokens) {
            int res = 0;
            int num1 , num2;
            int size = tokens.size();
            stack<int> st;
            for(int i = 0 ; i < size ; i++){
                string token = tokens[i];
                if(isOp(token[0]) && token.length() == 1){
                    op(token[0] , st);
                }else{
                    st.push(parseInt(token));
                }
            }
            return st.top();
        }
    };
  • 相关阅读:
    3.21上午
    3.17下午
    2017.4.14-morning
    2017.4.13-afternoon
    2017.4.13-morning
    2017.4.12-afternoon
    2017.4.12-morning
    2017.4.11-afternoon
    2017.4.11-morning
    2017.4.10-afternoon
  • 原文地址:https://www.cnblogs.com/x1957/p/3487611.html
Copyright © 2011-2022 走看看