leetcode 1021 刪除最外層的括號

class Solution {
public:
    stack<int>_stack;
    vector<int> pos;
    string strs;
    string removeOuterParentheses(string S) {
        
        strs = "";
        for(int i = 0 ; i < S.size(); i++)  
        {
            if( S[i] == '(' ) 
            {
                if(_stack.empty())
                {
                    pos.push_back(i);
                }
                _stack.push(i);
            }
            else if(S[i] == ')')
            {
                if(_stack.size() == 1)
                {
                    pos.push_back(i);
                    
                   
                }
                 _stack.pop();
               
            }
        }
        
        for(int i = 0 ; i < pos.size() ; i+=2)
        {
            string str = S.substr(pos[i] +1, (pos[i+1] - pos[i]) - 1  );
            strs+=str;
        }
        
        return strs;
        
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章