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 ...
随机推荐
- kubernetes发布tomcat服务,通过deployment,service布署
1.制作tomcat镜像 参考docker tomcat镜像制作 此处直接拉取 查看已有可镜像 先设置docker阿里源,即添加 "registry-mirrors": [&quo ...
- javascript页面刷新的几种方法
javascript refresh page 几种页面刷新的方法 本节内容:Javascript刷新当前页面的方法与实例. window.location.reload(),window.histo ...
- Lua脚本语法说明(转)
Lua脚本语法说明(增加lua5.1部份特性) Lua 的语法比较简单,学习起来也比较省力,但功能却并不弱. 所以,我只简单的归纳一下Lua的一些语法规则,使用起来方便好查就可以了.估计看完了,就懂得 ...
- JEECG新版UI规划,主要提供H5方案(采用主流技术)
JEECG 结合当前主流的UI技术,在新版3.7.4 即将推出新的UI方案,主要采用Bootstrap,Vue技术.同时提供代码生成器模板(单表.一对多),快速生成你喜好的模板代码. 版本一: Boo ...
- day25-面向对象结构与成员
1.面向对象结构分析 如下面的图所示:面向对象整体大致分两块区域: 每个大区域又可以分为多个小部分: class A: name = 'Tom' # 静态变量(静态字段) __iphone = '13 ...
- HTTP梳理
HTTP请求头 Host:初始URL中的主机名和端口 Accept:浏览器可接受的MIME类型 Acceept-Charset:浏览器接受的字符集 Accept-Encoding:浏览器能够进行解码的 ...
- GCD 常用API 总结
dispatch_sync:同步操作,会阻塞当前线程 dispatch_async:普通的异步操作,也就是在指定的队列中添加一个block操作,不会阻塞当前线程 dispatch_group_asyn ...
- 在Ubuntu 16.04 上编译安装OpenCV3.2.0(Cmake + python3 + OpenCV3)(转)
1 安装CMAKE sudo apt-get install cmake 2 安装python及其所依赖的软件包 sudo apt-get install build-essential sudo a ...
- Linux命令:chown
Linux命令:chmod https://baijiahao.baidu.com/s?id=1616750933810368135&wfr=spider&for=pc chmod - ...
- java由字符型强制转化为整型例题
此Java程序依次输出参数,参数类型为字符型,要求更改程序,使得字符型强制转化为整形,并将这些整数相加,最后输出总和. 原程序: package demo; public class CommandP ...