zoukankan      html  css  js  c++  java
  • Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution

     1 #define ADDITION  '+'
     2 #define SUBSTRACTION  '-'
     3 #define MULTIPLICATION  '*'
     4 #define DIVISION  '/'
     5 
     6 
     7 class Solution {
     8 public:
     9     set<char> tokenSet{'+', '-', '*', '/'};
    10     stack<int> num;
    11     int evalRPN(vector<string> &tokens) {
    12         for (auto &token : tokens) {
    13             auto iter = tokenSet.find( token[token.size()-1] );
    14             if ( iter == tokenSet.end() ) {
    15                 num.push( atoi( token.c_str() ) );
    16             } else {
    17                int right = num.top();
    18                num.pop();
    19                int left = num.top();
    20                num.pop();
    21                switch ( *iter )
    22                {
    23                    case ADDITION :
    24                        num.push(left + right);
    25                        break;
    26                    case SUBSTRACTION:
    27                        num.push(left - right);
    28                        break;
    29                    case MULTIPLICATION:
    30                        num.push(left * right);
    31                        break;
    32                    case DIVISION:
    33                        num.push(left / right);
    34                        break;
    35                    default:
    36                        break;
    37                }
    38             }
    39         }
    40         int ret = num.top();
    41         num.pop();
    42         return ret;
    43     }
    44 };
  • 相关阅读:
    设计模式
    设计模式
    设计模式
    JS | Reduce
    JS | 数组的深拷贝与浅拷贝
    JS | 数组操作
    Lodash | 指定路径对Object操作
    Git | 场景总结
    ES6 Class
    SpringBoot | Jpa @Id @GeneratedValue
  • 原文地址:https://www.cnblogs.com/ydlme/p/4295722.html
Copyright © 2011-2022 走看看