[LeetCode OJ]Generate Parentheses

来源:互联网 发布:淘宝开店怎么上传宝贝图片 编辑:程序博客网 时间:2024/05/29 05:57

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:

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


class Solution {public:    vector<string> generateParenthesis(int n) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<string> ans;        getAns(n, 0, 0, "", ans);        return ans;    }private:    void getAns(int n, int pos, int neg, string temp, vector<string> &ans) {        if (pos < neg) {            return;        }        if (pos + neg == 2 * n) {            if (pos == neg) {                ans.push_back(temp);            }            return;        }        getAns(n, pos + 1, neg, temp + '(', ans);        getAns(n, pos, neg + 1, temp + ')', ans);    }};


http://www.cnblogs.com/remlostime/archive/2012/11/06/2757711.html

http://blog.csdn.net/yangliuy/article/details/41170599

http://blog.csdn.net/pickless/article/details/9141935

0 0