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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
public class Solution {
private List<String> list = new ArrayList<String>(); public List<String> generateParenthesis(int n) {
run("", n, 0);
return list;
} // l 为剩余左括号数,r为剩余未匹配的右括号数目
public void run(String str, int l, int r) {
if (l == 0 && r == 0) {
list.add(str);
return; //跳出run方法
}
if (l > 0) {
String s = str + "(";
run(s, l-1, r+1);
}
if (r > 0) {
String s = str + ")";
run(s, l, r-1);
}
}
}

程序结果:

LeetCode 22. Generate Parentheses的更多相关文章

  1. [LeetCode] 22. Generate Parentheses 生成括号

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

  2. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  3. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  4. [leetcode]22. Generate Parentheses生成括号

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

  5. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

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

  6. [LeetCode] 22. Generate Parentheses ☆☆

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

  7. [LeetCode]22. Generate Parentheses括号生成

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

  8. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

  9. [leetcode] 22. Generate Parentheses(medium)

    原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...

随机推荐

  1. 【Beta】Scrum09

    Info 考试周,暂停工作 时间:2016.12.26 21:35 时长:20min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.12.31 21:30 Task Rep ...

  2. ios相同版本升级

    公司新发布ios,因为各种错误以及审核不通过造成app未能上传,而app在提交的时候不能上传相同的版本号,造成app还没有正式的上传上传已经将版本号升级到1.0.6 解决方法: 在Xcode上,查看项 ...

  3. SQLite

      什么是SQLite SQLite是一款轻型的嵌入式数据库 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 它的处理速度比Mysql.PostgreSQL这两款著名的数据库都还快 ...

  4. chose.jquery 联动

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

  5. 【先定一个小目标】怎么解决mysql不允许远程连接的错误

    最近使用Navicat for MySQl访问远程mysql数据库,出现报错,显示“1130 - Host'xxx.xxx.xxx.xxx' is not allowed to connect to ...

  6. Git学习笔记

    1.第一次提交文件的过程:     1)创建代码库:cd到要作为git代码库的文件夹下,执行git init命令.     2)设置邮箱和用户名:设置后才可提交代码,git config user.e ...

  7. 如何改变span元素的宽度与高度

    内联元素:也称为行内元素,当多个行内元素连续排列时,他们会显示在一行里面. 内联元素的特性:本身是无法设置宽度和高度属性的,但是可以通过CSS样式来控制,达到我们想要的宽度和高度. span举例1: ...

  8. Maven模块聚合

    一个Maven工程中一般会有很多模块组成,为了构建的方便通常想一次构建多个模块,Maven聚合这一特性就是为该需求服务的. 假设我们有account-email和account-persist两个模块 ...

  9. php数组array_push()和array_pop()以及array_shift()函数

    <?php /** * array_push()将一个或多个单元压入数组的末尾(入栈) */ $stack = array("Java", "Php", ...

  10. Spring Boot 乐观锁加锁失败 - 使用AOP恢复错误

    之前写了一些辅助工作相关的Spring Boot怎么使用AOP.这里继续正题,怎么减少Spring Boot 乐观锁加锁报错的情况(基本可以解决). 1. 包依赖 spring-boot-starte ...