LeetCode----301. Remove Invalid Parentheses----Hard

2018.3.25  Spring is coming 

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note: The input string may contain letters other than the parentheses ( and ).

Examples:

"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]

先說結果吧,這道題目我使用的是BFS,效率比較低。

使用的方法有點不夠正規,這裏記錄一下,以後可能會更新更好更通用的方法。

思路分析:

如果把'(' 看成 +1 而把 ')'看成-1 , 這樣的話 , 符合條件的括號序列首先要滿足 整一個序列之和爲0

例如()() ----> +1 -1 +1 -1 

序列之和爲0僅僅是一個必要條件 ,因爲  )()( ------> -1 +1 -1 +1之和也爲0,但是不是正確的括號序列

所以正確的括號序列還應加上一個條件:從第一個開始加,直到加到最後一個 ,其 結果都不能小於0,

()())( ------>+1 -1 +1 -1 -1 +1 從第一個開始加到最後一個的和: 

+1   ---> 0----->+1 -----> 0 ------> -1 ----->小於0了 所以一定不符合要求 

有了這個基本的思路 我就可以進行BFS搜索了 

1.將括號和字母的序列 存成 一個vector<int> 左括號存成 1 右括號存成-1,字母就存它本身的ASCII碼

2.對初始序列進行判斷 ,如果本身就已經是合理序列 就直接輸出即可 , 如果本身是錯誤序列,說明需要Remove掉一些括號

3.對錯誤的序列而言,將其狀態可以轉化成若干個狀態 , 例如:

()())()的情況,---->+1 -1 +1 -1 -1 +1 -1 它可以演變成以下幾個狀態

1)刪除第一個括號----->-1 +1 -1 -1 +1 -1

2)刪除第二個括號----->+1 +1 -1 -1 +1 -1

3)刪除第三個括號-----> +1 -1 -1 -1 +1 -1

等等,當然要避免序列重複的情況 ,我是簡單地將其放到一個set中來判斷

4.對每一個演變出來的新序列判斷其是否滿足要求,一旦發現滿足要求,就記錄下當前序列的長度,因爲我們需要找到所有滿足要求的序列,這些序列有一個特點,就是長度肯定是一樣的,一旦發現新序列的長度比這個記錄的長度短的,那說明找完了,下面貼上我的渣渣代碼,日後再來改-。-

class Solution {
public:
    vector<string> removeInvalidParentheses(string s) {
        int size = s.size();
        queue<vector<int>> Q ;
        vector<int> num ;
        vector<vector<int>> ans ;
        set<vector<int>> ss ;
        int lastSize = INT_MIN;
        bool isSuccess = true ;
        
        vector<string> solution ;
        
        for(int i = 0 ; i < size ;i++)
        {
            if(s[i] == '(')
                num.push_back(1);
            
            else if(s[i] == ')')
                num.push_back(-1);
            
            else 
                num.push_back(s[i]);
        }
        int count = 0 ;
    
        for(int i = 0 ; i < num.size() ; i++)
        {
            if(num[i] == 1 || num[i] == -1)
            {
                count += num[i] ;
                if(count < 0)
                {
                    isSuccess = false ;
                }
            }
        }
        if(count == 0 && isSuccess == true)
        {
            ans.push_back(num);
        }
        else {
        Q.push(num);
        bool isFound = false ;
        while(Q.empty() == false)
        {
            
            vector<int> temp = Q.front();            
            Q.pop();
            
            if(isFound == true && temp.size() == lastSize)
            {
                break;
            }
            
            for(int i = 0 ; i < temp.size() ; i++)
            {
                isSuccess = true ;
                vector<int> fuzhi(temp);
                vector<int>::iterator deletei = fuzhi.begin();
                count = 0 ;
                for(int j = 0 ; j < temp.size() ; j++)
                {
                    //如果不是括號,是字母的話,就可以跳過
                    if(temp[j] == 1 || temp[j] == -1)
                    {
                        if(j != i)
                        {
                            count += temp[j] ;
                            if(count < 0)
                                isSuccess = false ;
                        }
                    }
                    else 
                        continue;
                }
                if(fuzhi[i] == 1 || fuzhi[i] == -1)
                    fuzhi.erase(deletei+i);
                if(count == 0 && isSuccess == true)
                {
                    isFound = true ;
                    lastSize = fuzhi.size();
                    if(ss.insert(fuzhi).second == true)
                    {
                        Q.push(fuzhi);
                        ans.push_back(fuzhi);
                    }
                }
                else if(ss.insert(fuzhi).second == true)
                    Q.push(fuzhi);
                
                
            }
            
            
        }
        }
        if(ans.size() != 0)
        {
            for(int i = 0 ; i < ans.size() ; i++)
            {
                string temps ;
                for(int j = 0 ; j < ans[i].size() ; j++)
                {
                    if(ans[i][j] == 1)
                    {
                        temps.append(1,'(');
                    }
                    else if(ans[i][j] == -1)
                    {
                        temps.append(1,')');
                    }
                    else 
                    {
                        temps.append(1,ans[i][j]);
                    }
                }
                solution.push_back(temps);
            }
            return solution ;
        }
        solution.push_back("");
        return solution ;
    }
};
未完待續...

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