题目: 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的更多相关文章

  1. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  2. Java基础复习笔记系列 五 常用类

    Java基础复习笔记系列之 常用类 1.String类介绍. 首先看类所属的包:java.lang.String类. 再看它的构造方法: 2. String s1 = “hello”: String ...

  3. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  4. leetcode第21题--Generate Parentheses

    problem: Given n pairs of parentheses, write a function to generate all combinations of well-formed ...

  5. 《ASP.NET Core In Action》读书笔记系列五 ASP.NET Core 解决方案结构解析1

    创建好项目后,解决方案资源管理器窗口里我们看到,增加了不少文件夹及文件,如下图所示: 在解决方案文件夹中,找到项目文件夹,该文件夹又包含五个子文件夹 -Models.Controllers.Views ...

  6. LeetCode 笔记系列 18 Maximal Rectangle [学以致用]

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  7. 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 ...

  8. Python基础笔记系列五:元组

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 元组 1)元组的结构和访问.使用方法和列表基本一致,区别主要有两点:1.使 ...

  9. LeetCode(22)Generate Parentheses

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

随机推荐

  1. mysql 插入 详解

    表创建好后,就可以往里插入记录了,插入记录的基本语法如下: INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,…… ...

  2. java项目部署后的文件路径获取

    //eclipse部署工程 String path = request.getServletContext().getRealPath( File.separator+ "WEB-INF&q ...

  3. Swift 的 pod 第三方库

    #HTTPpod 'Alamofire' #Elegant HTTP Networking in Swiftpod 'SwiftHTTP' #Thin wrapper around NSURLSess ...

  4. SQL2005数据库行列转换

    注意:列转行的方法可能是我独创的了,呵呵,因为在网上找不到哦,全部是我自己写的,用到了系统的SysColumns (一)行转列的方法 先说说行转列的方法,这个就比较好想了,利用拼sql和case wh ...

  5. 应用SVN比较文件定位修改

    用SVN checkout一个版A本到一个目录中,再从别的地方找到版本B复制到版本A所在的目录中,选择复制和替换,再要比较的文件上右键-->SVN-->diff

  6. hdu 4217Data Structure?

    树状数组+二分 就是找第几小的数,,找几次,再求和. . #include<cstdio> #include<cstring> #include<iostream> ...

  7. android BaseAdapter getView 理解

    ListView是安卓中很经常使用的一个控件. 安卓设计使用Adapter来对ListView进行管理. 可是系统提供的Adapter无法满足一些复杂的显示情况,这个时候我们就须要使用BaseAdap ...

  8. 进程控制函数(1)-getpgid() getpgrp() 获取当前进程的进程组ID

    定义:pid_t getpid(void); 表头文件:#include<unistd.h> 说明:getpid()用来取得目前进程的进程识别码, 许多程序利用取到的此值来建立临时文件, ...

  9. dfs带状态改变的做法

    所谓带状态改变是指:在搜索到某个位置的时候,状态发生改变,继续计算步数. 给一个例题: 蒜头君要回家,但是他家的钥匙在他的朋友花椰妹手里,他要先从花椰妹手里取得钥匙才能回到家.花椰妹告诉他:“你家的钥 ...

  10. love2d教程32--碎图打包器texturepacker

    texturepacker是一个碎图打包器,可以把小图合并成一张大图,并对大图做优化.我用的是特别版(只好用xx版了, 不然导出的图片会被变成红色),网盘下载,文件会提示有毒,我也是在网上找的,其实是 ...