打印包含n个括号的所有合法的组合
2013-05-06 23:14
549 查看
Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses.
打印包含n个括号的所有合法的组合
打印包含n个括号的所有合法的组合
#include <iostream> using namespace std; const int n = 100; char a[n * 2]; void print(int l, int r, int i) { if(l < 0 || r < l){ return; } if (l==0 && r==0) { for (int j = 0; j < n * 2; j++) { if(a[j]==')' || a[j]=='(') cout << a[j] << " "; else break; } cout << endl; } else { if (l > 0) { a[i] = '('; print(l - 1, r, i + 1); } if (r > l) { a[i] = ')'; print(l, r - 1, i + 1); } } } int main() { int count; cin >> count; print(count, count, 0); return 0; }
相关文章推荐
- 判断括号字符串是否为合法+求n对括号的所有组合
- 打印所有匹配括号组合
- 输出所有的合法的括号组合
- 打印括号的合法组合
- 打印出所有可能的括号组合
- 【卡特兰数】有N对括号,输出所有合法的组合
- 打印所有合法的括号序列
- 9.9递归和动态规划(六)——打印n对括号的所有有效组合(即左右括号正确配对)
- 给一个字符串(包含重复字符),打印它的所有可能的组合。
- 打印n对括号匹配的所有组合
- 打印括号的所有组合
- 排列组合-打印出一个数组的元素的所有排列方式-算法
- 9.9递归和动态规划(六)——打印n对括号的全部有效组合(即左右括号正确配对)
- 编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- 卡塔兰数应用----所有括号的组合
- 打印目录下所有的文件名(包含深层次目录)
- Generate Parentheses 产生所有括号组合 @LeetCode
- 百度2014校园招聘算法——给出一组数据A=[a_0, a_1, a-2, ... a_n](其中n可变),打印出该数值元素的所有组合。
- 【算法】打印N对合理括号组合
- 面试编程题拾遗(06) --- 打印n对括号的全部有效组合