Generate Parentheses

来源:互联网 发布:面部识别软件 编辑:程序博客网 时间:2024/06/05 05:34

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:void process(int left, int right, string path, vector<string> &res){if (0 == left && 0 == right){res.push_back(path);return;}if (left > 0)process(left - 1, right, path + "(", res);if (left < right)process(left, right - 1, path + ")", res);}    vector<string> generateParenthesis(int n){string path;vector<string> res;process(n, n, path, res);return res;    }};



0 0