前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【leetcode】Generate Parentheses

【leetcode】Generate Parentheses

作者头像
阳光岛主
发布2019-02-19 11:33:03
4390
发布2019-02-19 11:33:03
举报
文章被收录于专栏:米扑专栏米扑专栏

Question :  

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

Anwser 1 :

代码语言:javascript
复制
class Solution {
public:
    vector<string> generateParenthesis(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> vec;
        string str;
        addParen(vec,str, n, n);

        return vec; 
    }
    
    void addParen(vector<string> &vec, string str, int left, int right) {        
        if (left == 0 && right == 0) {
            vec.push_back(str);
            return;
        }

        if (left > 0) {
            addParen(vec, str + '(', left - 1, right);
        }

        if (right > left) {
            addParen(vec, str + ')', left, right-1);
        }        
    }
};

Anwser 2 :

代码语言:javascript
复制
class Solution {
public:
    void printPar(int l, int r, vector<string>& result, char* str, int idx)
    {
        if (l < 0 || r < l) return;
        if (l == 0 && r == 0)
        {
            str[idx] = '\0';
            result.push_back(string(str));
        }
        else
        {
            if (l > 0)
            {
                str[idx] = '(';
                printPar(l-1, r, result, str, idx+1);
            }
            if (r > l)
            {
                str[idx] = ')';
                printPar(l, r-1, result, str, idx+1);
            }
        }
    }
    
    vector<string> generateParenthesis(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> result;
        char* str = new char[2*n+1];
        printPar(n, n, result, str, 0);
        delete []str;
        return result;
    }
};

Anwser 3 :

代码语言:javascript
复制
class Solution {
public:
    vector<string> generateParenthesis(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> ans;
        if (n > 0) {
            generator(ans, "", 0, 0, n);
        }
        return ans;
    }
    
    // r/l: appearance of ) (
    void generator(vector<string> & ans, string s, int l, int r, int n) { 
        if (l == n) {
            ans.push_back(s.append(n-r, ')'));
            return;
        }
        generator(ans, s + '(', l+1, r, n);
        if (l > r) {
            generator(ans, s + ")", l, r+1, n);
        }
    }
};

参考推荐:

Generate Parentheses

LeetCode: Generate Parentheses

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013年05月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档