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 ...
随机推荐
- MySQL 服务正在启动 .MySQL 服务无法启动。系统出错。发生系统错误 1067。进程意外终止。
MySQL 服务正在启动 .MySQL 服务无法启动.系统出错.发生系统错误 1067.进程意外终止. 检查了一个晚上才发现是---配置问题 #Path to installation directo ...
- VScode+Flutter 开发继续踩坑
运行慢解决方法1:修改build.gradle,注释掉jcenter(),google().使用阿里的镜像.原因是jcenter google库无法访问到导致的问题.虽然我有万能的爬墙工具,开启全局代 ...
- 伴随我整十个年头的校内网,现名 人人网, 是不是要shut down 了
如题: 每天我都习惯性的登录人人网,虽然现在基本上已经看不到曾经的同学上线了,不过我还是有事没事的往上面post 一些出行的照片,没事无聊的时候上这个网上看看自己曾经的照片,虽然已经无人在线,但是自己 ...
- python 库 Numpy 中如何求取向量范数 np.linalg.norm(求范数)(向量的第二范数为传统意义上的向量长度),(如何求取向量的单位向量)
求取向量二范数,并求取单位向量(行向量计算) import numpy as np x=np.array([[0, 3, 4], [2, 6, 4]]) y=np.linalg.norm(x, axi ...
- windows常用DLL及作用
Kernel.dll:内存,硬盘灯硬件管理的相关函数. gdi32.dll:图形显示相关函数(LoadImage,GetPixel,StretchBlt,Ellipse). user32.dll:wi ...
- ambassador 学习五 配置文件简述
Ambassador 配置通过yaml 的定义文件 格式 apiVersion 版本,当前支持的版本为 ambassador/v0 kind 支持的类型,目前有Module AuthService R ...
- conan c&&c++ 包管理工具使用
测试使用的是JFrog Artifactory CE 进行的私有包管理,具体的安装可以参考相关文档 启动JFrog Artifactory CE 使用docker docker run -d -p 8 ...
- C# Async&Await
在async和await之前我们用Task来实现异步任务是这样做的: static Task<string> GetBaiduHtmlTAP() { //创建一个异步Task对象,内部封装 ...
- mysql索引优化续
(1)索引类型: Btree索引:抽象的可以理解为“排好序的”快速查找结构myisam,innodb中默认使用Btree索引 hash索引:hash索引计算速度非常的快,但数据是随机放置的,无法对范围 ...
- X.509证书的编码及解析:程序解析以及winhex模板解析
一.证书的整体结构:证书内容.签名算法.签名结果. 用ASN.1语法描述如下: Certificate::=SEQUENCE{ tbsCertificate TBSCertificate, signa ...