LeetCode括号生成22

数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。

 

示例:

输入:n = 3
输出:[
       "((()))",
       "(()())",
       "(())()",
       "()(())",
       "()()()"
     ]


来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/generate-parentheses
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Accepted:

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> res;
        string cur;
        int open = 0, close = 0;
        dfs(res, cur, open, close, n);
        return res;
    }
    vector<string> res;
    void dfs(vector<string>& res, string& cur, int open, int close, int n) {
        if(cur.size() == n*2) {
            if(isValid(cur)) {
                res.emplace_back(cur);
                return;
            }
        }
        if(open < n) {
            cur.push_back('(');
            dfs(res, cur, open+1, close, n);
            cur.pop_back();
        }
        if(close < n) {
            cur.push_back(')');
            dfs(res, cur, open, close+1, n);
            cur.pop_back();
        }
    }
    bool isValid(string str) {
        stack<char> s;
        for(int i = 0; i < str.length(); i++) {
            if (str[i] == ')') {
                char c = s.size() ? s.top() : '$';
                if(s.size()) s.pop();
                if(c != '(') return false;
            } else {
                s.push(str[i]);
            }
        }
        return s.size() == 0;
    }
};

Timeout:

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<char> s_vec;
        for(int i = 0; i < n; i++) {
            s_vec.emplace_back('(');
            s_vec.emplace_back(')');
        }

        dfs(0, s_vec.size()-1, s_vec);
        return res;
    }
    vector<string> res;
    void dfs(int first, int end, vector<char> s_vec) {
        if (first == end && check(s_vec)) {
            string tmp = "";
            for(auto ch : s_vec) {
                tmp += ch;
            }
            if (!isExisted(res, tmp))
                res.emplace_back(tmp);
        }
        for(int i = first; i <= end; i++) {
            swap(s_vec[i], s_vec[first]);
            dfs(first+1, end, s_vec);
            swap(s_vec[i], s_vec[first]);
        }
    }
    bool check(vector<char> s_vec) {
        stack<char> s;
        for(auto c:s_vec) {
            if((c - ')') == 0) {
                char top = s.size() ? s.top() : '#';
                if(s.size()) s.pop();
                if (top != '(') {
                    return false;
                }
            } else {
                s.push(c);
            }
        }
        return s.size() == 0;
    }
    bool isExisted(vector<string> res, string q) {
        vector<string>::iterator it;
        it = std::find(res.begin(), res.end(), q);
        return it != res.end() ? true : false;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值