Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int calPoints(vector<string>& ops) {
- stack<int>st;
- for(int i=0;i<ops.size();i++)
- {
- string s=ops[i];
- if(s=="C")
- {
- if(!st.empty()) st.pop();
- }
- else if(s=="D")
- {
- if(!st.empty())
- {
- int tp=st.top();
- st.push(2*tp);
- }
- }
- else if(s=="+")
- {
- if(st.size()>=2)
- {
- int a,b,c;
- a=st.top();
- st.pop();
- b=st.top();
- st.pop();
- c=a+b;
- st.push(b);
- st.push(a);
- st.push(c);
- }
- }
- else
- {
- int p=stoi(s);
- st.push(p);
- }
- }
- int sum=0;
- while(!st.empty())
- {
- sum+=st.top();
- st.pop();
- }
- return sum;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement