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 ...
随机推荐
- 安装tensorflow出现问题的解法
在ubuntu14.04用pip安装tensorflow-gpu 安装1.3.0遇到问题 1.安装tensorflow出现Cannot uninstall 'six'.问题的解法 https://bl ...
- memcached监控工具
最简单和最直接的方式是在启动memcached的时候加入-vv参数,从而在控制台打印每次客户端的请求和相应,这非常适合开发.另外一种较为直接的方式是通过telnet进行查看,例如:若server为本机 ...
- UML的学习
1.什么是UML? 统一建模语言(UML,英语:Unified Modeling Language)是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明.可视化.构建和编写一个正在开发的. ...
- Android学习笔记——log无法输出的解决方法和命令行查看log日志
本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论. 欢迎转载,转载请注明网址:http://www.cnblogs.com/JohnTsai/p/3983936.html. 知识 ...
- web.py开发
web.py需要使用python2.X,所以安装python版本2.7.9 web.py 是一个轻量级Python web框架,它简单而且功能强大 web.py安装 安装python (1)使用pip ...
- WebGL Matrix4(4*4矩阵库)
Matrix4是由<<WebGL编程指南>>作者写的提供WebGL的4*4矩阵操作的方法库,简化我们编写的代码.源代码共享地址,点击链接:Matrix4源代码. 下面罗列了Ma ...
- Sublime的插件Color Highlighter的安装方法
ColorHighlighter是一个显示选中颜色代码的视觉颜色的插件.如果您选择“# fff“,它将向您展示白色.ColorHighlighter支持所有CSS颜色格式,如Hex,RGB,HSL,H ...
- 关于python 的空的__init__.py文件的作用,可不可以删除,到底有没有用?
0.声明,本篇只讨论空__init__.py文件的情况,不顾前提非得说__init__.py文件里面也可以写东西的不在此讨论了范围之内,重点是个"空"字. 1.很多地方的文件夹都有 ...
- python使用paramiko自动化部署linux程序
使用paramiko模块,比os模块和command模块更加的兼容各种环境.后面两个只能在服务器本机 执行,此模块写得python文件无论是在本地还是服务器本身上运行,都能兼容. paramiko模块 ...
- 使用d3.v3插件绘制出svg图
众所周知,这个插件使用的svg技术,而IE8(包括IE8)之前的浏览器是不支持svg的 接下来看代码吧 从后台获取到带id和父id的目录数据[json格式] var module = requestU ...