题目描述:

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:

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

解题思路:

利用递归的思想。用left和right分别记录剩余的左,右括号数。

每次都首先把左括号放进字符串中。

放入右括号的条件是:右括号剩余的数目大于0,且左括号剩余数目小于右括号剩余数目。

当left和right都为0时表明字符串已形成,加入到List中。

代码如下:

public class Solution {
List<String> list = new ArrayList<String>();
public List<String> generateParenthesis(int n) {
if (n <= 0)
return list;
generate("", n, n);
return list;
}
public void generate(String s, int left, int right) {
if (left == 0 && right == 0) {
list.add(s);
return;
}
if (left > 0)
generate(s + '(', left - 1, right);
if (right > 0 && right > left)
generate(s + ')', left, right - 1);
}
}

Java [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. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

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

  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生成括号

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

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

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

  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括号生成

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

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

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

随机推荐

  1. splObjectStroge的作用,实例化一个数组

    PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候. PHP SPL SplObjectStorage类实现了Countable,Iterator, ...

  2. showModalDialog 超过问题

    a.aspx页面打开一个弹出模式对话框b.aspx. a.aspx 页面页面代码: function SetPlay() { window.showModalDialog('SetAdvertisin ...

  3. 快捷设置IE代理小工具

    时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...

  4. iOS中touches事件,addtarget ...action和GestureRecognizer详解

    刚学完uiview,uicontrol类,许多人知道 touchesBegain,touchesMoved,touchesEnd,GestureRecognizer的用途,但仔细考虑这些事件之间的关系 ...

  5. owa Your request can't be completed right now. Please try again later.

    Your request can't be completed right now. Please try again later.

  6. css3 简单界面动画

    asdasdasdasda asdasdasdasda

  7. python学习笔记24(路径与文件 (os.path包, glob包))

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法. >>> import os.path >>> path = '/home/ ...

  8. Mvc设计模型与三层架构

    Mvc(Model-View-Controller):是软件架构的一中设计模式,对软件进行分割成3个层次:视图.模型.控制. 实现对软件的一种动态的设计,并且容易对软件进行扩展.后期的修改,使某些程序 ...

  9. Educational Codeforces Round 5 B

    Problem B:http://codeforces.com/contest/616/problem/B B. Dinner with Emma 题意:一对夫妻要去餐厅吃晚饭,Emma 想去最豪华( ...

  10. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...