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 ...
随机推荐
- Android 建立AIDL的步骤
建立AIDL服务要比建立普通的服务复杂一些,具体步骤如下: (1)在Eclipse Android工程的Java包目录中建立一个扩展名为aidl的文件.该文件的语法类似于Java代码,但会稍有不同.详 ...
- solr4.3+tomcat入门部署
solr4.3的入门配置 目前阿帕奇官方仅推荐2个比较稳定的版本一个是4.3的版本,一个3.6的版本 3.6的版本没有用过,所以在此无涉及,下面就来说说solr4.3的入门配置 s ...
- 2-3 JAVA内存模型
- codeforce 462DIV2 C题
题意 给出一个只含有1和2的序列,有n个元素,可以选择一段区间进行翻转操作,求再反转后的最大非递减子序列的长度 分析 太菜了只想出了N^2的做法.序列只有1和2,那么每个非递减子序列都会有一个分界点, ...
- 【bzoj3670】[Noi2014]动物园
3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2080 Solved: 1107[Submit][Status ...
- PHP Curl请求Https接口
在请求http的时候只需要 file_get_contents("http://www.sojson.com/open/api/weather/json.shtml?city=$Positi ...
- El表达式 (先大致的记录下吧!以后慢慢深入)
参考:http://blog.csdn.net/eson_15/article/details/51264269 1.获取数据采用 ${标识符} 的形式 request.setAttribute(&q ...
- jquery表单数据反序列化为字典
.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1 ...
- linux安装JDK后发现系统带有openjdk的处理
1.JDK下载. 官网下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- 第九课,ROS仿真1
---恢复内容开始--- 1.stage simulator 它是一个轻量级的仿真软件,它的包名称是stage_ros,可以进入看看,其包含地图在子目录world下, 启动之: rosrun stag ...