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. fullcalendar案例一<原>

    fullcalendar是个很强大的日历控件,可以用它进行排班.排会议.拍任务,很直观,用户体验良好. 看下效果图: #parse("index/head.vm") <lin ...

  2. oracle数据库表空间的创建与使用

     以下操作请使用sys系统账号操作! 1. 查询物理存储的位置 SELECT tablespace_name, file_id, file_name, round(bytes / (1024 * 10 ...

  3. 訪问站点时仅仅是显示主页(index.jsp),没有请求连接数据库读取数据。

    两部曲: 1:在你的web.xml中的Struts2的核心过滤器的映射中添加 <filter-mapping>     <dispatcher>REQUEST</disp ...

  4. 添加信任站点和允许ActiveX批处理

    有两种写法 1.如果是用IP reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMa ...

  5. 符号arg含义

    argument of the maximum/minimum arg max f(x): 当f(x)取最大值时,x的取值 arg min f(x):当f(x)取最小值时,x的取值 表示使目标函数取最 ...

  6. 20 个 jQuery 和 CSS 的文本特效插件

    Jumble Text Effect Plugins Demo || Download Vticker – Vertical News Ticker With JQuery Plugin Demo | ...

  7. 12款优秀jQuery Ajax分页插件和教程

    在这篇文章中,我为大家收集了12个基于 jQuery 框架的 Ajax 分页插件,这些插件都提供了详细的使用教程和演示.Ajax 技术的出现使得 Web 项目的用户体验有了极大的提高,如今借助优秀的 ...

  8. selenium测试(Java)--截图(十九)

    package com.test.screenshot; import java.io.File; import java.io.IOException; import org.apache.comm ...

  9. Resource接口,及资源

    Resource介绍 编码的时候,除了代码本身,我们还需要对外部的资源进行处理.例如:URL资源.URI资源.File资源.ClassPath相关资源.服务器相关资源(VFS等)等等. 而这些资源的处 ...

  10. C# HttpRequest基础连接已经关闭: 接收时发生意外错误

    在c#中使用HttpWebRequest时,频繁请求一个网址时,过段时间就会出现“基础连接已经关闭: 接收时发生意外错误”的错误提示. 将webRequest的属性设置成下面的,经测试可以解决. we ...