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. <记录> Ubuntu16.04 安装Redis以及phpredis扩展

    Linux下安装Redis 1.获取redis资源 wget http://download.redis.io/releases/redis-4.0.8.tar.gz 2.解压 tar xzvf re ...

  2. day33-常见内置模块二(hashlib、shutil、configparse)

    一.hashlib算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 1.什么是摘要算法呢? 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一 ...

  3. iOS app bundle id

    每个app的bundle id是唯一的,不同开发者账号不能申请相同的bundle id,例如开发者账号B想用开发者A的bundle id,只能是开发者A将这个bundle id先删除,B才可以注册,否 ...

  4. Linux学习笔记--vim

    Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用. vim的三种模式 命令模式(Command mode),输入模式(Insert m ...

  5. 1test

    Tencent's outsize influence in China's online world is ballast that should steady it as it targets b ...

  6. python 如何注释

    一.单行注释     单行注释以#开头,例如:    print 6 #输出6 二.多行注释     (Python的注释只有针对于单行的注释(用#),这是一种变通的方法)     多行注释用三引号' ...

  7. Hadoop集群(二) HDFS搭建

    HDFS只是Hadoop最基本的一个服务,很多其他服务,都是基于HDFS展开的.所以部署一个HDFS集群,是很核心的一个动作,也是大数据平台的开始. 安装Hadoop集群,首先需要有Zookeeper ...

  8. selenium自动化测试安装,浏览器驱动版本对应

  9. jq控制select值为某个时选中

    $("#selectche_type option").each(function(){//用id ,用class好像不得,不知道为何 alert(2) if($(this).va ...

  10. Python教程_简介2

    人生苦短,我用Python--Life is short,you need Python. https://www.bilibili.com/video/av14184325/?p=101 Pytho ...