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. PHP + Nginx 在 Linux(centos7)系统下的环境搭建

    ( 选用的操作系统为 centos7 ) 01,安装 nginx => 请移步 https://www.cnblogs.com/lovling/p/9197572.html 02,下载 php  ...

  2. Java工具类DateFormatUtils详解

    日期和时间格式化实用程序和常量public static String format(Calendar calendar, String pattern) 说明:将日历格式化为特定的模式:参数:cal ...

  3. 如何使用navicat远程连接服务器上的oracle数据库

  4. 20.struts2的数据填充和类型转换.md

    目录 1. struts2的自动填充 2. struts2的对象填充 3. struts2的类型转换器 3.1 类继承关系 3.2 局部转换器 3.3 全局转换器 3.4 注意 1. struts2的 ...

  5. shiro 密码的MD5盐值加密

  6. git 分支合并develop 重新拉取

    1.本地包 zb-local 上传到对应的线上分支 zb-online cd 进入本地所在文件夹,然后执行下面三项 git add . git commit -m '修改' git push   或者 ...

  7. python os模块的使用(转)

    os模块包含普遍的操作系统功能. 注意:函数参数path是文件或目录的路径,filename是文件的路径,dirname是目录的路径,路径可以是相对路径,也可绝对路径 常见或重要的函数为加粗字体 os ...

  8. MD5 算法

    MD5 Message Digest Algorithm MD5(中文名为消息摘要算法第 五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 1321(R. ...

  9. linux下网络配置

    配置相关 http://bbs.acehat.com/thread-813-1-1.html

  10. git---远程仓库版本回滚

    开发中,发现有错误版本提交带远程分支master,怎么处理? 1 简介 最近在使用git时遇到了远程分支需要版本回滚的情况,于是做了一下研究,写下这篇博客. 2 问题 如果提交了一个错误的版本,怎么回 ...