22. Generate Parentheses (backTracking)
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)的更多相关文章
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 22. Generate Parentheses (recursion algorithm)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- 使用jquery刷新当前页面、刷新父级页面
如何使用jquery刷新当前页面 下面介绍全页面刷新方法:有时候可能会用到 window.location.reload(); //刷新当前页面.(我用的这个一个,非常好) parent.locati ...
- Mac上python2和python3的版本切换
在命令行执行 vi ~/.bash_profile 在文件下面加上: alias python2='/system/Library/Frameworks/Python.framework/Versio ...
- zipfile模块
在python中操作zip文件, 基本上都是使用zipfile模块,他可以创建.解压文件,获取zip文件的元数据信息. 我们想要操作一个zip文件,第一步就是初始化ZipFile实例. 1.打开tes ...
- elasticsearch-java
elastissearch的JAVA客户端 官网 java api文档 https://www.elastic.co/guide/en/elasticsearch/client/java-api/ ...
- ueditor富文本框图片显示
修改config.json /* 前后端通信相关的配置,注释只允许使用多行方式 */ { /*"physicsPath":"E:/Software/apache-tomc ...
- 原生java读取存储为xml格式的数据,并存储到java bean里
一.举例读取的文件为:X-bond可交易债券信息_20180917.xml <?xml version="1.0" encoding="UTF-8"?&g ...
- YCSB性能测试工具使用(转)
在网上查In-Memory NoSQL性能测试的资料时,偶然间发现了这个性能测试工具YCSB,全称为“Yahoo! Cloud Serving Benchmark”.它内置了对常见NoSQL数据库和数 ...
- 用C#创建XML, XML格式化输出
demo: XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0&q ...
- 打包制作 ANE
一.打包ANE 1.ios 准备文件: anePackager.bat aneswc.swc extension.xml flashAne.ane ioslib.a library.swf platf ...
- RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World(转载)
RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World 一.简介 RabbitMQ是一个消息的代理器,用于接收和发送消息,你可以这样想,他就是一个邮局,当您把需要寄送的邮件投递到 ...