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 ...
随机推荐
- ajax 两者有什么不同
$.ajax({ type:"POST", url:url, //dataType:"json" ...
- 一个页面中内嵌页面 iframe元素
iframe.html: <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...
- Bypassing iPhone Code Signatures
[Bypassing iPhone Code Signatures] Starting with the recent beta releases of the iPhoneOS, Apple has ...
- 相关度算法BM25
BM25算法,通常用来作搜索相关性平分.一句话概况其主要思想:对Query进行语素解析,生成语素qi:然后,对于每个搜索结果D,计算每个语素qi与D的相关性得分,最后,将qi相对于D的相关性得分进行加 ...
- 02.socket实现远程调用
不使用webservice使用以前的知识也可以实现远程系统之间的调用.用Socket可以.实现Socket通信. 开设一个端口.ip.
- Python学习笔记_操作Excel
Python 操作Exel,涉及下面几个库: 1.xlrd 读取Excel文件 2.xlwt 向Excel文件写入,并设置格式 3.xlutils 一组Excel高级操作工具,需要先安装xlrd和xl ...
- zlib编程
一.简介 zlib是提供数据压缩用的函式库,使用DEFLATE算法,最初是为libpng函式库所写的,后来普遍为许多软件所使用,今天,zlib是一种事实上的业界标准. 二.基本信息 数据头(hea ...
- CopyOnWriteArrayList原理
http://blog.csdn.net/chayangdz/article/details/76347465 总结的很到位: http://www.cnblogs.com/java-zhao/p/5 ...
- WCF把书读薄(3)——数据契约、消息契约与错误契约
上一篇:WCF把书读薄(2)——消息交换.服务实例.会话与并发 十二.数据契约 在实际应用当中数据不可能仅仅是以int Add(int num1, int num2)这种简单的几个int的方式进行传输 ...
- try-catch-finally对返回值的影响
catch 和 finally 一起使用的常见方式是:在 try 块中获取并使用资源,在 catch 块中处理异常情况,并在 finally 块中释放资源. finally 块用于清理try块分配的任 ...