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();
        }
    };
  • 相关阅读:
    UIView添加手势
    UIView常见属性设置汇总
    关于页面传值
    有关segue的简介
    alloc
    如何定义静态方法
    一座小城
    清明
    开通博客
    iOS学习之界面间传值
  • 原文地址:https://www.cnblogs.com/x1957/p/3487611.html
Copyright © 2011-2022 走看看