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 ...
随机推荐
- Custom Ribbon in SharePoint 2010 & which not wrok when migrate from 2010 to 2013
博客地址 http://blog.csdn.net/foxdave 1. First of all, let me show you the ribbon modal in our project w ...
- FOR UPDATE
1. for update的使用场景 `如果遇到存在高并发并且对于数据的准确性很有要求的场景,是需要了解和使用for update的. 比如涉及到金钱.库存等.一般这些操作都是很长一串并且是开启 ...
- SVN的 安装
关于svn的安装,如图分别是服务端和客户端 下载地址:http://subversion.apache.org/packages.html 1.双击VisualSVN,点下一步,选择目录,就可以,很简 ...
- 利用xpath来解析douban电影相对应的信息
from lxml import etree import requests url = "https://movie.douban.com/chart" headers = {& ...
- spring-security-4 (4)spring security 认证和授权原理
在上一节我们讨论了spring security过滤器的创建和注册原理.请记住springSecurityFilterChain(类型为FilterChainProxy)是实际起作用的过滤器链,Del ...
- .NET移动开发环境搭建
开发工具:Xamarin Studio 社区版 下载地址 http://www.monodevelop.com/download/ 操作系统要求:Windows7及以上..NET Framework4 ...
- pthread线程初始化(pthread_once)
pthread_once 语法 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); #include ...
- Httpclient 支持https(转)
参考:https://jingyan.baidu.com/article/154b46317353d228ca8f4112.html 参考:https://www.jianshu.com/p/a444 ...
- 桶排序与快速排序算法结合-python实现
#-*- coding: UTF-8 -*- import numpy as np from QuickSort import QuickSort def BucketSort(a, n): barr ...
- vue 整合雪碧图功能
1.通过命令新建一个vue项目 环境要求: 安装有 Node.js. vue. vue-cli . 创建项目: vue init webpack tx_demo cd tx_demo 进入项目,下载依 ...