26.Generate Parentheses(生产有效括号的种类)
Level:
Medium
题目描述:
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代表左括号和右括号的剩余数,初始值为n,利用回溯的思想解题,当出现left的值大于right的值时,说明串中的右括号多于左括号,()),这种直接错误返回,如果出现left和right都为零则是满足情况的一个串。
代码:
public class Solution{
public List<String>generateParenthesis(int n){
List<String>res=new ArrayList<>();
if(n<=0)
return res;
int left=n;
int right=n;
String str="";
help(left,right,res,str);
return res;
}
public void help(int left,int right,List<String>res,String str){
if(left<0||right<0||right<left)
return;
if(left==0&&right==0){
res.add(str);
return;
}
help(left-1,right,res,str+"(");
help(left,right-1,res,str+")");
}
}
26.Generate Parentheses(生产有效括号的种类)的更多相关文章
- Generate parentheses,生成括号对,递归,深度优先搜索。
问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)
Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
随机推荐
- springMVC的多文件的异步上传实现
springMVC的MultipartFile与传统的ajax文件上传兼容性不好,采用如下的ajax方法,后台无法获取文件. $.ajax({ url: '/upload', type: 'POST' ...
- linux上的第一个c语言程序
1.编辑源文件 输入命令如下: root@ubuntu:/home/amy# vi hello.c 文件内容如下: #include<stdio.h> int main() { print ...
- sqlplus客户端出现乱码
查询oracle server端的字符集 select userenv('language') from dual; 修改客户端 cmd是gbk编码 环境变量 NLS_LANG 值:SIMPLIFIE ...
- 为cscope查找列表增添色彩
我在使用cscope的时候,偏好于不用quickfix窗口来显示查找列表,而是选择类似ctag的列表.但这会带来一个比较麻烦的问题,就是窗口列表一片白色,看起来非常难受: 特别是当搜索结果特别多的时候 ...
- 不要向没权力&能力的人证明自己的能力
[不要向没权力&能力的人证明自己的能力] 不是所有的上级都有足够的权力和能力.一个没权力的Leader,即使你向他证明了自己的能力,你所能获得的也只能是他的邮件表扬的荣誉.对于加薪,他能给的仅 ...
- java基础之io流总结四:字符流读写
字符流读写只适用于字符文件. 基本字符流(转换流)读写文件 转换流本身是字符流,但是实例化的时候传进去的是一个字节流,所以叫做转换流 InputStreamReader isr = new Input ...
- java基础之JDBC四:事务简单示例
/** * 事务 */ public class Test { public static void main(String[] args) { Connection conn = null; Pre ...
- linux设置rsync+inotify实时同步文件
linux设置rsync+inotify实时同步文件 应用场景: 同步接收方:test01 接收目录:/opt/software/test/a/ 同步发起方:test02 同步目录:/opt/so ...
- 733. Flood Fill 简单型染色问题
[抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value ...
- 696. Count Binary Substrings统计配对的01个数
[抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...