022 - Generate Parentheses
2015-11-11 18:22
316 查看
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:
给定一个数字,列出所有括号组合
void fun(int left, int right, char *line, int lp, char **result, int *returnSize)
{
if (left == 0) {
while (right--)
line[lp++] = ')';
memcpy(result[(*returnSize)++], line, lp);
return ;
}
if (left == right) {
line[lp++] = '(';
return fun(left - 1, right, line, lp, result, returnSize);
}
line[lp++] = '(';
fun(left - 1, right, line, lp, result, returnSize);
line[lp - 1] = ')';
fun(left, right - 1, line, lp, result, returnSize);
}
char** generateParenthesis(int n, int* returnSize)
{
int left = n, right = n;
char line[1024] = {0};
int lp = 0;
char **result = (char **)calloc(sizeof(char *), 8192);
int i;
for (i = 0; i < 8192; i++)
result[i] = (char *)calloc(sizeof(char), n + 1);
*returnSize = 0;
fun(left, right, line, lp, result, returnSize);
return result;
}
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
给定一个数字,列出所有括号组合
void fun(int left, int right, char *line, int lp, char **result, int *returnSize)
{
if (left == 0) {
while (right--)
line[lp++] = ')';
memcpy(result[(*returnSize)++], line, lp);
return ;
}
if (left == right) {
line[lp++] = '(';
return fun(left - 1, right, line, lp, result, returnSize);
}
line[lp++] = '(';
fun(left - 1, right, line, lp, result, returnSize);
line[lp - 1] = ')';
fun(left, right - 1, line, lp, result, returnSize);
}
char** generateParenthesis(int n, int* returnSize)
{
int left = n, right = n;
char line[1024] = {0};
int lp = 0;
char **result = (char **)calloc(sizeof(char *), 8192);
int i;
for (i = 0; i < 8192; i++)
result[i] = (char *)calloc(sizeof(char), n + 1);
*returnSize = 0;
fun(left, right, line, lp, result, returnSize);
return result;
}
相关文章推荐
- nova-compute不能成功启动的原因总结
- 021 - Merge Two Sorted Lists
- 大数据
- java 中 如何让一个方法内最多只能指定数量线程访问
- UE4.9 Hardware & Software Specifications
- 在java代码中设置字体大小,可以指定单位为sp
- 关于SMO算法的一点点认识
- cygintl-8.dll 是cygwin的哪个包?
- linux: 可重入函数与不可重入函数
- Java动态代理一——动态类Proxy的使用
- mysql自动备份脚本
- MKL在Visual Studio 2010中配置
- Epic Games Shows Incredible New Anti-Aliasing Technique – Screenshots + Video Comparison
- Java.util.regex中matcher 和pattern的用法
- mac下filezilla中找回存储的站点秘密
- openstack平台接管vmware现有虚拟机
- git 失效
- jquery实现当页面滚动超过一屏时显示返回顶部按钮
- 泛型方法的局限及解决之道
- Android获取设备电池信息,玩些新花样