题目: 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. python--迭代器的实现

    #!/usr/local/python/bin/python3 """ 一个迭代器的例子 """ class exsample(object ...

  2. Linux更改Apache网站目录出错:Document root must be a directory解决

    Linux更改Apache网站目录出错:Document root must be a directory解决   修改   DocumentRoot     <Directory " ...

  3. php调试函数

    void debug_print_backtrace ([ int $options = 0 [, int $limit = 0 ]] ) array debug_backtrace ([ int $ ...

  4. CSS3多背景应用

    /*多背景应用*/ .wrapper { width: 640px; height: 1000px; margin: auto; background: url(./images/head.jpg) ...

  5. java中的类加载器ClassLoader和类初始化

    每个类编译后产生一个Class对象,存储在.class文件中,JVM使用类加载器(Class Loader)来加载类的字节码文件(.class),类加载器实质上是一条类加载器链,一般的,我们只会用到一 ...

  6. Bootstrap学习笔记(2)--栅格系统深入学习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Linux下HTTP Server

    想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...

  8. 《高性能MySQL》读书笔记(1)

    慢查询 当一个资源变得效率低下的时候,应该了解一下为什么会这样.有如下可能原因:1.资源被过度使用,余量已经不足以正常工作.2.资源没有被正确配置3.资源已经损坏或者失灵 因为慢查询,太多查询的实践过 ...

  9. 性能加速 - 开启opcache

    说明 PHP 5.5+版本以上的,可以使用PHP自带的opcache开启性能加速(默认是关闭的).对于PHP 5.5以下版本的,需要使用APC加速,这里不说明,可以自行上网搜索PHP APC加速的方法 ...

  10. Http缺省的请求方法是。(选择1项)

    A.PUT B.GET C.POST D.TRACE 解答:B