Implement an algorithm to print all valid (e. g. , properly opened and closed) combinations
of n-pairs of parentheses.
EXAMPLE:
input: 3 (e. g. , 3 pairs of parentheses)
of n-pairs of parentheses.
EXAMPLE:
input: 3 (e. g. , 3 pairs of parentheses)
output: ()()(), ()(()), (())(), ((())),(()())
两张方法都用了之前讲过的卡特兰数的原理
void genPar(vector<char> &vt,int sum, int beg, int end, int acc) {
//第0位置肯定放(,那么和这个左括号匹配的右括号可能的位置是2*i+1,然后按照此类方法再去处理括号里面的元素和括号外面的元素
vt[beg] = '(';
++acc;
if (beg + 1 == end && acc +1 == sum){
vt[end] = ')';
for (int j=0; j< sum; ++j)
cout<<vt[j];
cout<<endl;
return;
}
for(int i = beg + 1; i<= end; i +=2 ) {
int tmp = acc;
vt[i] = ')';
if (i - beg >=3)