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:

  1. [
  2. "((()))",
  3. "(()())",
  4. "(())()",
  5. "()(())",
  6. "()()()"
  7. ]

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

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

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

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

  1. class Solution {
  2. public List<String> generateParenthesis(int n) {
  3. /*
  4.  
  5. */
  6. List<String> list=new ArrayList<>();
  7. helper(list,"",0,0,n);
  8. return list;
  9. }
  10. /*
  11. 求n时的括号组合。
  12. left表示此时字符串中左括号的个数,right表示字符串中右括号的个数,
  13. */
  14. public void helper(List<String> list,String str,int left,int right,int n){
  15. if(str.length()==n*2){
  16. list.add(str);
  17. return ;
  18. }
  19.  
  20. //当left小于n时,表示还可以添加左括号,当left大于right时,表示可以添加右括号
  21. //注意:该字符串中,左括号的个数不可能小于右括号的个数,因为这样就是无效的输出。
  22. if(left<n)
  23. helper(list,str+"(",left+1,right,n);
  24. if(left>right)
  25. helper(list,str+")",left,right+1,n);
  26. }
  27. }
  1.  

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. 【java集合框架源码剖析系列】java源码剖析之java集合中的折半插入排序算法

    注:关于排序算法,博主写过[数据结构排序算法系列]数据结构八大排序算法,基本上把所有的排序算法都详细的讲解过,而之所以单独将java集合中的排序算法拿出来讲解,是因为在阿里巴巴内推面试的时候面试官问过 ...

  2. TCP/IP 协议简单分析

    首先TCP和IP是两种不同的协议,它们来七层网络模型中分别在不同的层次,IP协议是网络层的协议,TCP是更高一层的传输层的协议,TCP是建立在IP协议之上的,所以一般把TCP和IP连在一起说TCP/I ...

  3. Android的DatePicker和TimePicker-android学习之旅(三十八)

    DatePicker和TimePicker简介 DatePicker和TimePicker是从FrameLayout继承而来,他们都是比较简单的组件.时间改变时间分别添加OnDateChangeLis ...

  4. Dynamics CRM2013 注释中的内容无法正常显示问题

    CRM2013中在表单中插入注释,并把注释设置成默认选项卡后 打开一个已经挂了附件的表单,但却显示找不到记录 必须要再点击下注释,内容才会出来 查了半天不得其解,终于在ur1 for CRM2013  ...

  5. Integration between SharePoint 2013 and CRM 2013 (On-Premise)

    具体步骤可见下面的链接 https://community.dynamics.com/crm/b/msdynamicscrmtips/archive/2014/01/27/integration- ...

  6. 04-GIT TortoiseGit冲突和补丁演示 案例演示

    TortoiseGit安装下载 http://download.tortoisegit.org/tgit/1.8.12.0/ 或https://code.google.com/p/tortoisegi ...

  7. android:layout_alignleft layout_toleftof区别,详解RelativeLayout布局属性

    转载请注明博客地址. 最近看博客看到有关于RelativeLayout布局的解释,有的解释很多是错误的.因此有必要对每一个常见的布局属性进行描述.以下解释全部都是逐行进行测试的. 首先把常用的布局分组 ...

  8. C++对象模型(三):Program Transformation Semantics (程序转换语义学)

    本文是Inside The C++ Object Model Chapter 2 部分的读书笔记.是讨论编译器调用拷贝构造函数时的策略(如何优化以提高效率),侯捷称之为"程序转化的语义学&q ...

  9. 幂次法则power law

    幂次法则分布和高斯分布是两种广泛存在的数学分布.可以预测和统计相关数据. pig中用其处理数据倾斜,实现负载均衡. 个体的规模和其名次之间存在着幂次方的反比关系,R(x)=ax(-b次方) 其中,x为 ...

  10. AWR報告詳解

    AWR是Oracle  10g 版本 推出的新特性, 全称叫Automatic Workload Repository-自动负载信息库, AWR 是通过对比两次快照(snapshot)收集到的统计信息 ...