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:
"((()))", "(()())", "(())()", "()(())", "()()()"
解法:leetcode上的解法很赞。 其实这也是利用的递归的分支。构建了一树状结构并遍历,叶子节点就是valid的结果。
public static ArrayList<String> generateParenthesis(int n) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<String> result = new ArrayList<String>();
generate(result, "",0,0,n);
return result;
} private static void generate(ArrayList<String> result, String prefix, int leftCount, int rightCount,int totalPairs){
if(leftCount == totalPairs){
for(int i = 0; i < totalPairs - rightCount;i++){
prefix += ")";
}
result.add(prefix);
return;
}
generate(result, prefix + "(", leftCount + 1, rightCount, totalPairs);
if(leftCount > rightCount) generate(result, prefix +")", leftCount, rightCount + 1,totalPairs);
}
leftCount和rightCount分别记录当前高度(或者长度)中,“(“和“)”的数目。如果leftCount等于了总共要求的括号对数,我们就把剩余的右括号添加进去并作为一个结果记录下来。这个有一些类似二叉树的post order遍历。
值得学习的地方:
1.利用递归模拟组合操作,做有序搜索;
2.有效的算法来自简单的代码。
LeetCode 笔记系列五 Generate Parentheses的更多相关文章
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- Java基础复习笔记系列 五 常用类
Java基础复习笔记系列之 常用类 1.String类介绍. 首先看类所属的包:java.lang.String类. 再看它的构造方法: 2. String s1 = “hello”: String ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- leetcode第21题--Generate Parentheses
problem: Given n pairs of parentheses, write a function to generate all combinations of well-formed ...
- 《ASP.NET Core In Action》读书笔记系列五 ASP.NET Core 解决方案结构解析1
创建好项目后,解决方案资源管理器窗口里我们看到,增加了不少文件夹及文件,如下图所示: 在解决方案文件夹中,找到项目文件夹,该文件夹又包含五个子文件夹 -Models.Controllers.Views ...
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- Python基础笔记系列五:元组
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 元组 1)元组的结构和访问.使用方法和列表基本一致,区别主要有两点:1.使 ...
- LeetCode(22)Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
随机推荐
- screen常用命令
1. 背景 由于经常使用ssh登录实验室的服务器训练神经网络, 而一些复杂的神经网络模型需要长时间训练,在此期间,如果出现网络等原因出现链接中断的话,服务器的进程也会被杀死,之前的一切半途而废.利用s ...
- Atitit. 解决unterminated string literal 缺失引号
Atitit. 解决unterminated string literal 缺失引号 原因:::或许string没使用引号括号起来...missingMessage缺失了一个单个的引号 Error: ...
- KVM部署LVS集群故障案例一则
一.故障现象 KVM部署LVS(Linux Virtual Server)集群后,能够单独以HTTP方式访问RS(Real Server)的实际IP,但无法通过VIP(Virtual IP)访问. 二 ...
- swift 单例模式
class ca{ ; static var instance:ca = ca(); class func GetInstance()->ca{ return instance; } } var ...
- 模板中的 TemplateBinding 问题
昨天一个朋友向我求助一个自定义水印控件绑定的问题,问题出在文本框中输入的文本,不能绑定到 相应的依赖属性上(自定义的依赖属性 PassText),他纠结了很久找不出问题所在.问题帮他解决后,这里稍 做 ...
- Jmeter测试带加密参数的接口
在做接口测试时,很多时候我们都会碰到带有加密参数的接口,这种接口一般来讲都会有统一的加密方法,找开发要就好,Jmeter怎么去测呢 1.整体结构如下所示: 2.操作步骤 (1)将加密方法打成jar包放 ...
- Centos 挂载NTFS格式的USB硬盘
公司的一台服务器本身硬盘容量较小,要加一块2T的硬盘,用来做samba服务器,以下是挂载硬盘的主要记录: 这是服务器不认NTFS格式的情况:mount: unknown filesystem type ...
- 为什么对一些矩阵做PCA得到的矩阵少一行?
很多时候会出现把一个N*M的矩阵做pca(对M降维)之后却得到一个M*(M-1)矩阵这样的结果.之前都是数学推导得到这个结论,但是, 今天看到一个很形象的解释: Consider what PCA d ...
- 已知问题汇总 (2013-11-30) - QQ空间, EXTJS
目前发现两个已知问题暂时无法得到解决: 1. QQ空间问题. 打开页面 http://user.qzone.qq.com/822994792/311, 点击 "xxx人赞" 这个链 ...
- 让浏览器下载文件http头部
网站提供下载服务时经常需要实现一个强制下载功能(即强制弹出下载对话框),并且文件名保持和用户之前上传时相同. 效果如下图: Content-Disposition 使用 HTTP Header 的 ...