22.Generate Parentheses (String; Back-Track)
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
思路:两个递归函数,互相调用
class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n==) return ret;
dfsLeft("",,,n);
return ret;
}
void dfsLeft(string s, int depthLeft, int depthRight, int n){ //add one left parenthesis
if(depthLeft >= n){ //end left
return;
}
else{
s += '(';
depthLeft++;
dfsRight(s,depthLeft, depthRight, n);
dfsLeft(s,depthLeft, depthRight, n);
}
}
void dfsRight(string s, int depthLeft, int depthRight, int n){ //add one right parenthesis
if(depthRight >= n){ //end all
ret.push_back(s);
}
else if(depthRight >= depthLeft){ //end right
return;
}
else{
s += ')';
depthRight++;
dfsLeft(s,depthLeft, depthRight, n);
dfsRight(s,depthLeft, depthRight, n);
}
}
private:
int len;
vector<string> ret;
};
更简洁的写法:
class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n == ) return ret;
len = n*;
dfsLeft(n, , "");
return ret;
}
void dfsRight(int lNum, int rNum, string str){//add right parenthese
while(rNum){
str += ')';
dfsLeft(lNum, --rNum, str);
}
if(str.length() == len){
ret.push_back(str);
}
}
void dfsLeft(int lNum, int rNum, string str){//add left parenthese
while(lNum){
str += '(';
dfsRight(--lNum, ++rNum, str);
}
}
private:
int len;
vector<string> ret;
};
22.Generate Parentheses (String; Back-Track)的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 【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
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
随机推荐
- jmeter4.0---自带录制功能录制脚本
1.前言 Jmeter录制脚本有两种方式.1.通过第三方工具录制比如:Badboy,然后转化为jmeter可用的脚本:2.使用jmeter本身自带的录制脚本功能. 对于小白来说可用先使用jmeter录 ...
- Mac下 cordova 安装随笔
首先这是我自己第一篇博客,如果有什么不对的,大家指出,积极修改. cordova是大家做混合开发最经常使用的一款使用HTML, CSS & JS进行移动App开发多平台共用一套代码,中文官方网 ...
- Vim技能修炼教程(17) - 编译自己的Vim
编译自己的Vim 前面我们已经对Vim有比较丰富的了解了.我们也知道Vim有很多编译时的选项,很多功能依赖于这些编译选项.其中最重要的就是脚本语言的支持,很多发行版本是不全的.为了支持我们所需要的功能 ...
- C/C++ 父子进程之间的文件描述符问题
在C程序中,文件由文件指针或者文件描述符表示.ISO C的标准I/0库函数(fopen, fclose, fread, fwrite, fscanf, fprintf等)使用文件指针,UNIX的I/O ...
- 2018c语言第2次作业
1 删除字符串中数字字符 1.设计思路 (1)主要描述题目算法 第一步:先用for循环比较每个数是否符合删除条件. 第二步:如果符合就把这个数利用交换把这个数提前一位. 2.实验代码 void del ...
- IO流常规操作
IO流 IO就是输入输出,IO设备在计算机中起着举足轻重的作用,IO流也就是输入输出流,用来交互数据,程序和程序交互,程序也可以和网络等媒介交互. 一.IO流的分类 要分类,肯定得站得不同角度来看这个 ...
- 把CDLinux制作成U盘启动
因为用下了CDlinux,本来想在虚拟机上运行的.发现虚拟机跑的时候无法识别集成的笔记本网卡,坑爹啊.后来想刻碟的,发现手头上还没有现成的东西,光驱是只读的,又要用到光驱,于是想到了了用U盘,正好手上 ...
- OpenLTE安装教程
安装需求: USB3 interface Modern multicore CPU (Intel Core i5, Core i7 or equivalent with SSE4.1 SSE4.2 a ...
- 【linux】查看linux版本和内核版本
查看linux版本:uname -r 查看linux版本内核:lsb_release -a
- 智能家居入门DIY——【五、执行命令】
前面几篇介绍了ESP8266使用AT命令来连接WIFI实现一系列功能.这一篇介绍一下使用Wemos D1 Wifi来进行开发,当然也可以用常见的8针ESP8266来完成(只是需要按网上的方法将Ardu ...