leetcode3.26每日一题(682. 棒球比赛)

66 阅读1分钟

关于栈的认知题,会用栈了啥也好说。浅显的easy就不讲思路了。

class Solution {
public:
    int calPoints(vector<string>& ops) {
     stack<int> num;
     int sum = 0;
     for(string op:ops){
         if(op =="+"){
             int n1 = num.top();
             num.pop();
             int n2 = num.top();
             num.push(n1);
             num.push(n1+n2);
         }
         else if(op=="C")
             num.pop();
         else if(op=="D")
             num.push(num.top()*2);
         else 
             num.push(atoi(op.c_str()));
     }
     while(!num.empty()){
         sum += num.pop();
         num.pop();
     }
     return sum;
    }
};