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:
"((()))", "(()())", "(())()",
"()(())", "()()()"
#include<iostream> #include<vector> #include<string> using namespace std; void creatpath(vector<string>&Result,string &path,int lnum,int rnum) { if (lnum==0&&rnum==0){ Result.push_back(path); return; } if (lnum>0) creatpath(Result,path+"(",lnum-1,rnum+1); if (rnum>0) creatpath(Result,path+")",lnum,rnum-1); } vector<string> generateParenthesis(int n) { vector<string>Result; string path; creatpath(Result,path,n,0); return Result; }
原文地址:http://blog.csdn.net/li_chihang/article/details/44803523