Description:

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:

"((()))", "(()())", "(())()", "()(())", "()()()"

参考:http://blog.csdn.net/yutianzuijin/article/details/13161721

public class Solution {

    public void solve(int left, int right, String ans, List<String> res) {

        if(left == 0 && right == 0) {
res.add(ans);
} if(left > 0) {
solve(left-1, right, ans+"(", res);
} if(right>0 && left<right) {
solve(left, right-1, ans+")", res);
} } public List<String> generateParenthesis(int n) { List<String> list = new ArrayList<String>();
String ans = new String();
solve(n, n, ans, list); return list;
}
}

LeetCode——Generate Parentheses的更多相关文章

  1. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  2. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

  3. [LeetCode]Generate Parentheses题解

    Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...

  4. [LeetCode] Generate Parentheses 生成括号

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

  5. LeetCode Generate Parentheses (DFS)

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

  6. LeetCode: Generate Parentheses [021]

    [称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  7. LeetCode Generate Parentheses 构造括号串(DFS简单题)

    题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...

  8. leetcode Generate Parentheses python

    # 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...

  9. 并没有看起来那么简单leetcode Generate Parentheses

    问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...

随机推荐

  1. uboot中MAC网络(待续)

    start ->start_armboot ->main_loop 实际应用中问题:为什么从nandflash读出的MAC(写到物理phy上)与上层网口地址不同(上层网口采用env的mac ...

  2. copy src remote_src false表示本地,true在远程

    文件组装模块-assemble assemble主要是将多份配置文件组装为一份配置文件. 参数 必填 默认 选项 说明 Backup 否 No Yes/no 是否创建备份文件,使用时间戳 Delimi ...

  3. 深入浅出Cache

    章节 ① 什么是Cache? Cache的目标? ② Caching住哪些内容? ③ 我们想要的Cache产品 ④ Cache使用方式 ⑤ 对于总体系统的提高 ⑥ 关于Sharding ⑦ Cache ...

  4. Jquery判断某字符串中是否包含某个字符

    if(!(to_city_value.indexOf("(")>0){ //code..... }

  5. CSRF简单介绍及利用方法

    x00 简要介绍 CSRF(Cross-site request forgery)跨站请求伪造,由于目标站无token/referer限制,导致攻击者可以用户的身份完成操作达到各种目的.根据HTTP请 ...

  6. php 删除指定文件 glob,unlink

    我用最简单的语句写了一个php删除指定文件 因为一直越级挑战thinkphp,所以突然面对php有点无所适从了... 首先,php的运行机制,是一个语法就搞定了,还是非要用数据库,还是post什么的. ...

  7. 【转】【WPF】IvalueConverter和TypeConverter

    简要说明: IValueConverter主要用于XAML绑定和数据源之间的转换 TypeConverter主要用于自定义类的属性类型之间的转换 本文主要讲解如何使用IValueConverter和T ...

  8. ubuntu下android环境的搭建

    ---------省略1000字 https://dl-ssl.google.com/android/eclipse/  如果该方法被墙,直接下载最新ADT,在我的博客里有介绍,或者我已经上传百度网盘 ...

  9. (记录)eclipse常用设置步骤

    代码风格文件导入: https://blog.csdn.net/wangming520liwei/article/details/53911736 注释中的author修改: https://jing ...

  10. Python 爬虫批量下载美剧 from 人人影视 HR-HDTV

    本人比較喜欢看美剧.尤其喜欢人人影视上HR-HDTV 的 1024 分辨率的高清双字美剧,这里写了一个脚本来批量获得指定美剧的全部 HR-HDTV 的 ed2k下载链接.并依照先后顺序写入到文本文件, ...