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 ...
随机推荐
- New Concept English Two 26 70
$课文68 纠缠不休 712. I crossed the street to avoid meeting him, but he saw me and came running towards m ...
- Robot Framework中使用HttpLibrary教程and中文支持
Robot Framework中使用and转参数时,默认不支持中文模式,如图场景: 会出现这种错误 FAIL : UnicodeDecodeError: 'ascii' codec can't dec ...
- webpack实现修改代码实时刷新浏览器
webpack例子:https://github.com/Aquarius1993/webpackDemo 1. 需要全局和项目安装webpack和webpack-dev-server npm ins ...
- Swift 导航栏设置
let width = UIScreen.mainScreen().bounds.size.width let height = UIScreen.mainScreen().bounds.size.h ...
- pidgin-lwqq
今天第一次用pidgin登上了qq,好神奇. 从网上看到的,说是pidgin发布了支持webqq协议的lwqq,按照解释之后安装了. 感觉好神奇啊. 一个绿色的小企鹅^^ 今天有看到了一个大牛,我觉得 ...
- 最佳C/C++编辑器 source insight3
C/C++嵌入式代码编辑器source insight3下载地址 http://www.sourceinsight.com/eval.html 注册码:SI3US-361500-17409
- [LOJ535]「LibreOJ Round #6」花火
loj description 给你一个排列\(h_i\),你需要交换任意两个位置上的数使得交换后排列的逆序对数最少. \(n \le 3\times 10^5\) sol 首先可以发现,如果交换两个 ...
- Linux内核gpiolib注册建立过程
1.相关的数据结构 struct s3c_gpio_chip { // 这个结构体是三星在移植gpiolib时封装的一个结构体 用来描述一组gpio端口信息 struct gpio_chip chip ...
- 【转】vim环境设置和自动对齐
原文网址:http://blog.chinaunix.net/uid-23525659-id-4340245.html 注:如果是用vim编写代码,建议开启vim的文件类型自动检测功能,这样编写代码换 ...
- python实现判断一个字符串是否是合法IP地址
#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:判断一个字符串是否是合法IP地址 ''' import re def jud ...