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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

给定一个数字表示有多少对括号,根据这些括号生成括号集合。

这个题比较有难度。运用到了回溯算法。搞不太懂。

往字符串中加括号,在符合要求的情况下,可以加左括号或者右括号,所以在符合要求下对这两种情况进行尝试,在不符合时,就直接返回上一层继续探索(这里不作处理也就是返回上一次调用了)

题目给定n表示n对括号,就有n个左括号和n个右括号,我们定义两个变量left和right分别表示字符串中左括号的个数和右括号的个数。如果字符串中右括号的个数大于左括号的个数,这就会出现)(的情况,所以不作处理,直接返回。当左括号的个数小于n时,这个时候就可以添加左括号,右括号个数小于左括号时,可以添加右括号。也就是说,两种情况都可以,所以都进行尝试。当字符串长度和n*2相等时就可以添加到List中了。

class Solution {
public List<String> generateParenthesis(int n) {
/* */
List<String> list=new ArrayList<>();
helper(list,"",0,0,n);
return list;
}
/*
求n时的括号组合。
left表示此时字符串中左括号的个数,right表示字符串中右括号的个数,
*/
public void helper(List<String> list,String str,int left,int right,int n){
if(str.length()==n*2){
list.add(str);
return ;
} //当left小于n时,表示还可以添加左括号,当left大于right时,表示可以添加右括号
//注意:该字符串中,左括号的个数不可能小于右括号的个数,因为这样就是无效的输出。
if(left<n)
helper(list,str+"(",left+1,right,n);
if(left>right)
helper(list,str+")",left,right+1,n);
}
}
												

generate parentheses(生成括号)的更多相关文章

  1. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  2. [LintCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  3. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. [leetcode]22. Generate Parentheses生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. 022 Generate Parentheses 生成括号

    给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[  "((()))",  "(()())",  "(())() ...

  7. 22.Generate Parentheses[M]括号生成

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. LeetCode OJ:Generate Parentheses(括号生成)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 22. Generate Parentheses生成指定个括号

    生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...

随机推荐

  1. Volley,小并发网络请求的好帮手

    不得不说,当不了解一件事情的时候,就会像当然的认为,其很神秘.但是当真正的接触到了这些神秘的item,就不会有这种感觉了.作为一个android开发新手的我,刚接触到了Volley这个开源的网络请求框 ...

  2. java的list几种实现方式的效率(ArrayList、LinkedList、Vector、Stack),以及 java时间戳的三种获取方式比较

    一.list简介 List列表类,顺序存储任何对象(顺序不变),可重复. List是继承于Collection的接口,不能实例化.实例化可以用: ArrayList(实现动态数组),查询快(随意访问或 ...

  3. C语言--static修饰函数

    在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. 介绍它的第一条也是最重要的一条:隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性 ...

  4. Java进阶(三十三)java基础-filter

    java基础-filter 我们先看看没有filter的时候,整个web客户端-服务端的一个流程. 接下来我们再看看引入了filter之后的Uml图.尝试分析这两者之间的差别. filter从哪里来? ...

  5. MyBatis主键生成器KeyGenerator(一)

    Mybatis提供了主键生成器接口KeyGenerator,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过配置的方式来完成这个功能 . 由于 ...

  6. 多态原理探究-从C++编译器角度理解多态的实现原理

    理论知识: 当类中声明虚函数时,编译器会在类中生成一个虚函数表. 虚函数表是一个存储类成员函数指针的数据结构. 虚函数表是由编译器自动生成与维护的. virtual成员函数会被编译器放入虚函数表中. ...

  7. 设计比较好,有助于学习的Github上的iOS App源码 (中文)

    Github版 中文 : TeamTalk 蘑菇街. 开源IM. 电商强烈推荐. MyOne-iOS 用OC写的<一个> iOS 客户端 zhihuDaily 高仿知乎日报 Coding ...

  8. Linux中的端口占用问题

    本文将会阐述两种解决端口占用的方法. 本文会用到的服务器端的程序如下: #include "unp.h" #include <time.h> int main(int ...

  9. EFI怎么装系统? UEFI BIOS

    关于EFI的介绍,就不赘述了. 大家可以看看这个帖子 http://benyouhui.it168.com/thread-2488583-1-1.html 总之,新电脑都是这玩意,win8也做了相应E ...

  10. Dynamics CRM ADFS及IFD部署后延长系统注销时间

    Dynamics CRM 部署IFD后,一段时间后登陆状态会失效,系统会提示让你重新登陆,可以通过延长失效时间来规避 在 powershell中执行如下指令 Set-ADFSRelyingPartyT ...