括号问题

 

 

20. Valid Parentheses(括号匹配,用桟)

class Solution {
public:
    bool isValid(string s) {
        stack<char> stk;
        for (char c : s) {
            if (c == '(' || c == '[' || c=='{') {
                stk.push(c);
            }  else if (!stk.empty() && leftof(c)==stk.top()) {
                stk.pop();
            } else {
                return false;
            }
        }
        return stk.empty();
    }
    char leftof(char c) {
        if (c == ')') return '(';
        if (c == '}') return '{';
        if (c == ']') return '[';
        return c;
    }
};

 

22. Generate Parentheses(回溯)

class Solution {
public:
    vector<string> res;
        bool is_vilid(const string& str) {
        int balance = 0;
        for (char c : str) {
            if (c == '(') {
                ++balance;
            } else {
                --balance;
            }
            if (balance < 0) {
                return false;
            }
        }
        return balance == 0;
    }

    void dfs(string& path,int level,int n) {
         if (level>n ) { 
             return;
         }    
        if (level==n ) {
            if (is_vilid(path)) res.emplace_back(path);
            return;
        }
        string path_back = path;
        path.append("(");
        dfs(path,level+1,n);
        path.pop_back();

        path.append(")");
        dfs(path,level+1,n);
        path.pop_back();
        
    }
    vector<string> generateParenthesis(int n) {
        string path = "";
        dfs(path,0,n*2);
        return res;
    }
};

 



32. Longest Valid Parentheses(最长括号匹配,hard)

class Solution {
public:
    int longestValidParentheses(string s) {
        int res = 0;
        int l_cnt =0;
        int r_cnt = 0;
        for(int i = 0; i < s.size();i++) {
            if (s[i] == '(') l_cnt++;
            else r_cnt++;
            if (l_cnt==r_cnt) res = max(res,r_cnt*2);
            if (l_cnt<r_cnt) l_cnt = r_cnt = 0;
    
        }
        r_cnt = l_cnt = 0;
        for(int i = s.size()-1; i >= 0;i--) {
            if (s[i] == '(') l_cnt++;
            else r_cnt++;
            if (l_cnt==r_cnt) res = max(res,r_cnt*2);
            if (r_cnt<l_cnt) l_cnt = r_cnt = 0;   
        }
        return res;
    }
};

 

 

301. 删除无效的括号(dfs)

class Solution {
public:
    set<string> res;
    void dfs(string path,int level,string& s,int l_cnt, int r_cnt,int min_cnt) {
        if (l_cnt < r_cnt || l_cnt> min_cnt || r_cnt>min_cnt || level>s.size()) return;
        if (level == s.size()) {
            if (l_cnt==r_cnt && l_cnt == min_cnt) {
                res.insert(path);
            }
        }
        char c = s[level];
        if (c =='(') {
            dfs(path+c,level+1,s,l_cnt+1,r_cnt,min_cnt);
            dfs(path,level+1,s,l_cnt,r_cnt,min_cnt);
        } else if (c == ')') {
            dfs(path+c,level+1,s,l_cnt,r_cnt+1,min_cnt);
            dfs(path,level+1,s,l_cnt,r_cnt,min_cnt);
        } else {
            dfs(path+c,level+1,s,l_cnt,r_cnt,min_cnt);
        }
    }
    vector<string> removeInvalidParentheses(string s) {
        int l_cnt = 0;
        int r_cnt = 0;
        for(auto c: s) {
            if (c=='(') l_cnt++;
            else if (c==')') { // )( 避免这种case
                if (l_cnt > r_cnt) {
                r_cnt++;
                }
            }
        }
        int min_cnt = min(l_cnt,r_cnt);
        dfs("",0,s,0,0,min_cnt);
        vector<string> out;
        for(auto i : res) {
            out.emplace_back(i);
        }
        return out;
    }
};

 

678. 有效的括号字符串

class Solution {
public:
    bool checkValidString(string s) {
        int s_cnt = 0;
        int l_cnt =0;
        int r_cnt = 0;
        for(int i = 0; i < s.size();i++) {
            if (s[i] == '(') l_cnt++;
            else if (s[i]==')') r_cnt++;
            else s_cnt++;
            if (l_cnt + s_cnt <r_cnt) return false;
        }
        s_cnt = r_cnt = l_cnt = 0;
        for(int i = s.size()-1; i >= 0;i--) {
            if (s[i] == '(') l_cnt++;
            else if (s[i]==')') r_cnt++;
            else s_cnt++;
            if (r_cnt + s_cnt <l_cnt) return false;
        }
        return true;;
    }
};

921. 使括号有效的最少添加

class Solution {
public:
    int minAddToMakeValid(string s) {
        int l_cnt =0;
        int r_cnt = 0;
        int res = 0;
        for(int i = 0; i < s.size();i++) {
            if (s[i] == '(') l_cnt++;
            else  r_cnt++;
            if (r_cnt>l_cnt) {
                res++;
                l_cnt++;
            }
        }        
        r_cnt = l_cnt = 0;
        for(int i = s.size()-1; i >= 0;i--) {
            if (s[i] == '(') l_cnt++;
            else r_cnt++;
            if (l_cnt>r_cnt) {
                res++;
                r_cnt++;
            }
        }
        return res;
    }
};

 

posted @ 2022-03-06 13:37  乐乐章  阅读(25)  评论(0编辑  收藏  举报