[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 ...
随机推荐
- scrapy框架的日志等级和请求参数
一 . Scrapy的日志等级 - 在使用 scrapy crawl xxx 允许程序时,在终端里打印输出的就是scrapy的日志信息 - 日志信息的种类 : ERROR : 错误信息 WARNING ...
- asp.net(c#)GridView实现鼠标悬停高亮显示
搜索了一下,发现对于这种基本都使用girdview中的RowDataBound事件完成,下面看看网上提供的简单方法protected void GridView1_RowDataBound(objec ...
- ARP数据包伪造
一台网络中的计算机,其传递到网络中的数据包的内容是完全由其软硬件逻辑决定的,软件可以操控硬件,硬件亦是一种特殊的软件,所以,接收者只根据数据包的内容,绝不可能判定此数据包的真正来源,一切都是可以伪 ...
- HTML怎么让img 等比例缩放
在img标签里面只设置宽,不设置高,图片就会等比例缩放.
- UNITY 画布的粗浅理解
画布:当画布是screen-space overlay时,这个好理解,画布可以控制如分辨率,层次等.但当画布是 world-space时,这个严格来说就不算是一个画布了,屏幕空间或相机空间的画布是先绘 ...
- ResultMap详解
MyBatis:ResultMap详解 一.前言 MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或BCNF,但实际事与愿违,那么结果集映射就是MyBat ...
- 插入后获取到id
第一种方法: insert INTO student(name) VALUES("南亚");SELECT @@identity 第二种方法: insert INTO student ...
- Django框架开发web网站的网页优化—页面静态化
网站优化-页面静态化 1)概念 提前将页面所用到的数据从数据库查询出来,然后生成一个静态页面,之后用户来访问的时候,直接返回静态页面. 举例:首页静态化:获取首页用到的数据表中的数据,生成静态首页in ...
- linux shell 脚本攻略学习 -- head命令详解, tail命令详解
当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...
- ssh框架,工具类调用service层方法
解决方法: @Component//声明为spring组件 public class CopyFileUtil{ @Autowired private DataFileManager dataFile ...