美文网首页
24. Generate Parentheses FROM Le

24. Generate Parentheses FROM Le

作者: 时光杂货店 | 来源:发表于2017-03-16 17:43 被阅读7次

    题目

    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:

    [
    "((()))",
    "(()())",
    "(())()",
    "()(())",
    "()()()"
    ]

    频度: 4

    解题之法

    class Solution {
    public:
        vector<string> generateParenthesis(int n) {
            vector<string> res;
            addingpar(res, "", n, 0);
            return res;
        }
        void addingpar(vector<string> &v, string str, int n, int m){
            if(n==0 && m==0) {
                v.push_back(str);
                return;
            }
            if(m > 0){ addingpar(v, str+")", n, m-1); }
            if(n > 0){ addingpar(v, str+"(", n-1, m+1); }
        }
    };
    

    相关文章

      网友评论

          本文标题:24. Generate Parentheses FROM Le

          本文链接:https://www.haomeiwen.com/subject/zsmjnttx.html