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 ...
随机推荐
- js 正则去重
split():字符串中的方法,把字符串转成数组. sort():数组中的排序方法,按照ACALL码进行排序. join():数组中的方法,把数组转换为字符串 var demo="ababb ...
- linux中kill命令
Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须 ...
- mysql用户管理 常用sql语句 mysql数据库备份恢复
- 自定义控件?试试300行代码实现QQ侧滑菜单
Android自定义控件并没有什么捷径可走,需要不断得模仿练习才能出师.这其中进行模仿练习的demo的选择是至关重要的,最优选择莫过于官方的控件了,但是官方控件动辄就是几千行代码往往可能容易让人望而却 ...
- 通过tarball形式安装HBASE Cluster(CDH5.0.2)——配置分布式集群中的YARN ResourceManager 的HA
<?xml version="1.0"?> <!-- Licensed under the Apache License, Version 2.0 (the &q ...
- 【python】并行化的又一种思路
https://segmentfault.com/a/1190000000414339
- linux下主流的三种远程技术
远程登录操作对于租用服务器的用户来说都不陌生.特别是租用国外服务器的用户来说,更是家常便饭.通过远程登录操作,即使我们人在深圳,也能无差别的操作远在美国的服务器.而对于linux系统下的服务器,目前主 ...
- 常见bootloader介绍
https://blog.csdn.net/weibo1230123/article/details/82716818 http://fasight001.spaces.eepw.com.cn/art ...
- linux proc目录介绍
https://www.cnblogs.com/DswCnblog/p/5780389.html
- Ajax请求全局配置
摘要: jQuery已经成为项目中最常见的js库,也是前端开发最喜欢使用的库.下面是在项目中封装了jQuery的Ajax,分享给大家. 代码: // ajax 请求参数 var ajaxSetting ...