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:

"((()))", "(()())", "(())()", "()(())", "()()()"

/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
char** generateParenthesis(int n, int* returnSize) {
char** returnArray = NULL;
if(n==) return returnArray; char* elem = malloc(sizeof(char)*(n*+));
returnArray = malloc(sizeof(char*)*); backTracking(n,,elem, , returnArray, returnSize);
return returnArray;
} /*
*@parameter
*left(in): number of left parenthesis to add
*right(in): number of right parenthesis to add
*/
void backTracking(int left, int right, char* elem, int pElem, char** returnArray, int* returnSize ){
int i, j, pTmp;
//逐一填(,然后逐一填),每次都要回溯
for(i = ; i < left; i++){ //fill (
elem[pElem] = '(';
pElem++;
pTmp = pElem;
for(j = ; j <= i+right; j++){ //fill )
elem[pTmp] = ')';
pTmp++;
backTracking(left-i,i+right-j,elem, pTmp, returnArray, returnSize);
}
} //最后,是只填了(的情况,那么一次性填写所有的)
elem[pElem] = '(';
pElem++;
for(i = ; i <= right+left; i++){
elem[pElem] = ')';
pElem++;
}
elem[pElem] = '\0';
char* returnElem = malloc(sizeof(char) * (pElem+));
memcpy(returnElem, elem, sizeof(char) * (pElem+));
returnArray[*returnSize] = returnElem;
(*returnSize)++;
}

22. Generate Parentheses (backTracking)的更多相关文章

  1. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  2. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  3. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  4. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  5. 22. Generate Parentheses (recursion algorithm)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. [leetcode]22. Generate Parentheses生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

随机推荐

  1. RabbitMq (1)

    1.传递模型 点对点模型(PTP) 发布-订阅模型 -------------------------------------------------------------------------- ...

  2. DOS 格式化日期时间输出

    if "%date:~5,2%" lss "10" (set mm=0%date:~6,1%) else (set mm=%date:~5,2%)if &quo ...

  3. 机器学习入门-数值特征-连续数据离散化(进行分段标记处理) 1.hist(Dataframe格式直接画直方图)

    函数说明: 1. .hist 对于Dataframe格式的数据,我们可以使用.hist直接画出直方图 对于一些像年龄和工资一样的连续数据,我们可以对其进行分段标记处理,使得这些连续的数据变成离散化 就 ...

  4. SpringBoot配置发送邮件

    一.导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  5. python 之列表推导式,集合推导式,以及字典推导式

    https://www.cnblogs.com/weihengblog/p/8428124.html

  6. Erlang 笔记

    集成开发环境:IntelliJ IDEA的Erlang插件 教程:www.erlang-cn.com/462.html,寻找erlang程序设计第2版pdf f():释放之前绑定过的所有变量. -ex ...

  7. Servlet基本_Httpリクエスト、レスポンス

    1.リクエスト リクエストは.リクエストライン.メッセージヘッダ.改行.メッセージボディで組まれる. 主なリクエストヘッダは. Accept クライアントが利用可能なデータメディアタイプを指定. Ac ...

  8. mysql / pgsql 使用sql语句查询数据库所有表注释已经表字段注释

    mysql使用sql语句查询数据库所有表注释已经表字段注释(转载)   场景: 1. 要查询数据库 "mammothcode" 下所有表名以及表注释 /* 查询数据库 ‘mammo ...

  9. HashMap 实现总结

    Entry类中需包含键值的hash值,防止resize时的重复计算: Map容量为2的整幂,可使用位操作取代取余操作提高效率: resize时需要将原table的桶内数据置null,利于垃圾回收: h ...

  10. WEB前端问题——img标签的onclick事件无法响应问题【转载】

    一个纠结了一下午的问题,img标签里面的onclick事件无法响应.最终找到了错误原因,是因为img标签的id与onclick事件的方法名相同. 于是接着又测试了一下,发现name名和方法名相同也会导 ...