[LeetCode 题解]: Generate Parentheses
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
题解: 很容易想到DFS, 采用递归的方式进行,比较难把握的是每次递归的结果用什么来存储,终止条件是什么。
需要注意的是,在每次递归的时候剩余左括号不可能比右括号多,否则就终止。
class Solution {
public:
vector<string> vi;
void generateOne(int left,int right,string s)
{
if(left==)
{
vi.push_back(s+string(right,')'));
return ;
}
if(left>=right)
generateOne(left-,right,s+'(');
else
{
generateOne(left-,right,s+'(');
generateOne(left,right-,s+')');
}
}
vector<string> generateParenthesis(int n) {
int left=n,right=n;
vi.clear();
generateOne(n,n,"");
return vi;
}
};
转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!
[LeetCode 题解]: Generate Parentheses的更多相关文章
- leetcode题解 Generate Parentheses
原文链接:https://leetcode.com/problems/generate-parentheses 给出数字n,求n对括号组成的合法字符串. 刚做出来,敲完代码,修改了一次,然后提交,ac ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- leetcode之 Generate Parentheses
题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...
- [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 ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- U3D中的又一个坑
using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; pu ...
- 如何有效地学习《空中英语教室》&《彭蒙惠英语》
读者定位: <大家说英语>是学习美式口语入门书,内容全部是情境会话,定位为“初级美式生活会话”. <空中英语教室>以浅显英语提供从新闻.旅游到时尚等流行话题,丰富会话材料,定位 ...
- shell编程——变量的数值计算
在shell脚本中,有时候会需要对数值类型的变量进行计算,通常我们用的是(()) [root@localhost collect]# ((a=1+2)) [root@localhost collect ...
- 依赖VUE组件通讯机制实现场景游戏切换
- 利用Fiddler对Android模拟器网络请求进行抓包
安装使用Fiddler 下载安装Fiddler的方法这里就略过了,一路Next就行了.装好之后运行软件,正常情况这个时候我们已经可以对电脑的网络请求进行抓包了.Fiddler默认的代理地址是127.0 ...
- 2-R型聚类
将相似的属性聚合在一起 clc, clear; % a = load('E:\a-建模\<数学建模算法与应用>课件资源\数学建模算法与应用\程序及数据\10第10章\gj.txt'); a ...
- 万能头文件#include<bits/stdc++.h>
最近在打cf时赛后翻阅别人的代码总是会发现一个陌生而奇怪的头文件#include<bits/stdc++.h> 奇怪之处就在于基本上所有的代码只要用了这个头文件就不再写其他头文件了. 百度 ...
- 如何启动jdeveloper中集成的weblogic
1>运行jdeveloper,打开运行日志,入下图,日志最开始的红框部分就是打开weblogic的命令,将此命令复制出来执行即可打开weblogic 程序员的基础教程:菜鸟程序员
- Fix “Could Not Find This Item” When Deleting in Windows 7
If you’ve been using Windows for as long as I have, you have probably run into your share of weird e ...
- CENTOS 使用 MUTT发送邮件
有些时候我们需要在Centos服务器上发送邮件,例如备份MySQL数据库并发送到指定邮箱,这里我们就说下如何从Centos的shell命令发送邮件. 检查.安装.启动sendmail //检查 ps ...