LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
package leetcode_50; import java.util.ArrayList;
import java.util.List; /***
*
* @author pengfei_zheng
* n对合理括号结果求解问题
*/
public class Solution22 {
public static List<String> generateParenthesis(int n) {
List<String> list = new ArrayList<>();
backtrack(list,"",0,0,n); return list;
}
private static void backtrack(List<String> list,String str, int open, int close, int max) {
if(str.length()==2*max){
list.add(str);
return;
}
if(open<max)
backtrack(list,str+"(",open+1,close,max);
if(close<open)
backtrack(list,str+")",open,close+1,max);
}
public static void main(String[]args){
List<String> list = generateParenthesis(3);
System.out.println(list);
}
}
LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)的更多相关文章
- Leetcode22. Generate Parentheses(生成有效的括号组合)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:
- [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/ 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 ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 22. Generate Parentheses产生所有匹配括号的方案
[抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- [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 ...
随机推荐
- 使用w查看系统负载 vmstat命令 top命令 sar命令 nload命令
w/uptime 查看系统负载 w查看系统负载,uptime跟w一样. [root@centos7 ~]# w 22:34:10 up 6 days, 23:10, 4 users, load a ...
- IOS_多线程
苹果的Cocoa框架支持的多线程机制有三中NSThread.GCD.NSOperation. NSThread:是官方推荐的也是最主要的线程创建方式,可是须要开发这自己去管理线程的生命周期比如线程同步 ...
- sql产生随机数
使用RAND(),结果是类似于这样的随机小数:0.615942003695649 SELECT FLOOR(RAND()*N) ---生成的数是这样的:12.0 SELECT CAST(FLOOR( ...
- Adb 获取手机信息
adb shell getprop [ro.product.board]: [herring][ro.product.brand]: [google][ro.product.cpu.abi2]: [a ...
- 怎么用ABBYY在线浏览PDF文件
ABBYY FineReader 让您可以从在线存储服务中打开图像或 PDF 文件,并将已识别文本保存至在线存储服务中,如 Dropbox.SkyDrive 或 Google Drive 等.通过在 ...
- Python爬虫-爬取科比职业生涯高清图集
前面学习了Python爬取豆瓣电影Top250的数据,爬取的信息是电影信息的文本信息,但是在互联网上流行的图片才有更大的吸引力,本篇我们来使用python爬取网页上的图片并保存在本地硬盘上,很兴奋吧, ...
- windows 环境内网超快同步 DFS
记录下: 在WINDOWS环境下,内网同步使用DFS可以超快实现文件同步,效果非常OK 纯粹记录下!
- mysql中json_object函数的使用?
需求说明: 今天看了json_object函数的使用,在此记录下使用过程 操作过程: 1.使用json_object函数将一个键值对列表转换成json对象 mysql> select json_ ...
- docker machine介绍和使用
https://www.cnblogs.com/sparkdev/p/7044950.html https://www.jianshu.com/p/cc3bb8797d3b
- linux 按照端口一句命令杀死进程,按照进程名称一句命令杀死进程
例如杀死nginx 按照程序名称杀死进程 例如杀死nginx的进程 ps -aux|grep nginx|grep -v grep|cut -c 9-15|xargs kill -9 或者 ps -a ...