Advertisement
sajid161

11:3

Jan 8th, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int calPoints(vector<string>& ops) {
  4.      stack<int>st;
  5.         for(int i=0;i<ops.size();i++)
  6.         {
  7.             string s=ops[i];
  8.             if(s=="C")
  9.             {
  10.                 if(!st.empty()) st.pop();
  11.  
  12.  
  13.             }
  14.             else if(s=="D")
  15.             {
  16.                 if(!st.empty())
  17.                 {
  18.                      int tp=st.top();
  19.                     st.push(2*tp);
  20.                 }
  21.  
  22.             }
  23.             else if(s=="+")
  24.             {
  25.                 if(st.size()>=2)
  26.                 {
  27.                         int a,b,c;
  28.                         a=st.top();
  29.                         st.pop();
  30.                         b=st.top();
  31.                         st.pop();
  32.                         c=a+b;
  33.                         st.push(b);
  34.                         st.push(a);
  35.                         st.push(c);
  36.                 }
  37.  
  38.             }
  39.             else
  40.             {
  41.                 int p=stoi(s);
  42.                 st.push(p);
  43.             }
  44.         }
  45.         int sum=0;
  46.         while(!st.empty())
  47.         {
  48.             sum+=st.top();
  49.             st.pop();
  50.         }
  51.        return sum;
  52.     }
  53. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement