Java [leetcode 22]Generate Parentheses
题目描述:
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的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- [LeetCode] 22. Generate Parentheses ☆☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode]22. Generate Parentheses括号生成
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode] 22. Generate Parentheses(medium)
原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...
随机推荐
- AvalonDock 2.0 的简单运用
最近在研究AvalonDock的一些使用,碰到了一些问题.现在拿出来跟大家分享分享. 网上找了一大把AvalonDock 1.3版本的资料,弄出Demo后发现属性面板(DockableContent) ...
- 统计网卡TX(发送)RX(接受)流量脚本
显示网卡流量的方法蛮多,一般我们可以通过dstat来查看,但dstat不一定所有的机器都有安装.而我们知道,通过ifconfig可以看到某一网卡发送与接收的字节数,所以我们可以写一个脚本来统计一下. ...
- iOS6 以上设置文本高度,行高(转)
2013-12-09 我来说两句 来源:冻僵的企鹅'zone 收藏 我要投稿 在iOS 7之前,常用下面这个方法计算文本高度sizeWithFont:constrainedToS ...
- 运行windows系统工具命令
appwiz.cpl 卸载/安装程序 wscui.cpl 操作中心 inetcpl.cpl 查看Internet属性 eventvwr 查看监视消息和疑难解答消息 taskmgr 任 ...
- Asp.Net MVC结合ExtJs gridPanel 分页和高度自适应
Ext.onReady(function () { gridPanel(); var panel = Ext.getCmp('gridPanel'); window.onresize = functi ...
- 4.2 spring-import 标签的解析;
对于spring配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件.不过,分模块都是大多数人能想到的方法,但是,怎么分模块,那就是仁者见仁,智者见智了.我的策略是使用im ...
- 【技术贴】删除360快捷搜索 ctrl+ctrl
恶心的功能,这么变态!如何删除360快捷键ctrl,桌面跳出360搜索怎么办?360 ctrl 删除 卸载方法: 桌面右下角,在360图标上右键点击设置,进入设置中心. 把 [开启快捷搜索功能,双击C ...
- hdu 1028
递推 #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> ...
- java基本对象Integer,String比较相等及equal案例说明
Integer i = new Integer(100); Integer i2 = new Integer(100); if(i == i2){ System.out.println("A ...
- java thread类和runable
java thread 类其实和其他类是一样的.有自己的属性和方法.以及有一个重写的run方法 不同的是:必须重写run()方法. run()方法是线程thread启动后cpu调用运行的程序代码. r ...