题目

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:

"((()))", "(()())", "(())()", "()(())", "()()()"

代码

For 2, it should place one "()" and add another one insert it but none tail it,

'(' f(1) ')' f(0)

or add none insert it but tail it by another one,

'(' f(0) ')' f(1)

Thus for n, we can insert f(i) and tail f(j) and i+j=n-1,

'(' f(i) ')' f(j)

public List<String> generateParenthesis(int n) {List<String> result = new ArrayList<String>();if (n == 0) {result.add("");} else {for (int i = n - 1; i >= 0; i--) {List<String> insertSub = generateParenthesis(i);List<String> tailSub = generateParenthesis(n - 1 - i);for (String insert : insertSub) {for (String tail : tailSub) {result.add("(" + insert + ")" + tail);}}}}return result;
}

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No22.Generate Parentheses相关推荐

  1. 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)

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

  2. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

  3. 【LeetCode从零单排】No118 Pascal's Triangle

    题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...

  4. 【LeetCode从零单排】No70.ClimbingStairs

    题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...

  5. 【LeetCode从零单排】No38.CountAndSay

    题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111 ...

  6. 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

    题目      题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...

  7. 【LeetCode从零单排】No.7 Reverse Integer

    前话       今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目        没想到做的第一道题目,虽然看似 ...

  8. LeetCode 22. 括号生成(Generate Parentheses)

    题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: ["((()))","(()() ...

  9. 【LeetCode从零单排】No118 Pascal#39;s Triangle

    题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...

最新文章

  1. python100行代码-python代码行数统计 100行
  2. QBC(Query By Criteria) QBE (Query By Example)
  3. python怎么用for循环找出最大值_如何获取Python简单for循环索引
  4. css3实现的精美菜单
  5. 真的掏空了吗?华为开始疯狂出4G手机
  6. linux系统环境与文件权限
  7. Java开发笔记(六十九)泛型类的定义及其运用
  8. Django学习笔记7
  9. go语言读取文件的方法-1
  10. OpenGL超级宝典(第7版)之第九章片段处理与帧缓冲
  11. 东芝166打印机装驱动出现inf安装服务段落无效
  12. simulink enable嵌套问题
  13. 【find128】项目
  14. java练手代码大全手机版_Java版打字练习游戏源码
  15. 计算机丢失iggy,我似乎已经丢失了什么
  16. LSTM长短记,长序依赖可追忆(深度学习入门系列之十四)
  17. javascript实现下拉条联动_js实现select二级联动下拉菜单
  18. gantt客制化gantt 图标
  19. 黑客是如何进行IP欺骗的
  20. c++语言中break的作用,c++ 中break的用法介绍

热门文章

  1. PAT甲级1029 Median:[C++题解]贪心、二路归并
  2. PAT甲级1024 Palindromic Number:[C++题解]回文串和高精度加法
  3. 通信网络基础期末复习-第四章-多址接入协议
  4. 兔子的区间密码-最大异或对
  5. leetcode 703. 数据流中的第K大元素 最小堆解法 c语言
  6. 燕赵志愿云如何认证_如何成为中国志愿服务网注册志愿者?操作秘籍!
  7. 12无法使用otg_ios设备该如何选择U盘,以及U盘日常使用技巧
  8. 计算机组成原理答案郑秋梅,计算机组成原理-郑秋梅_习题.docx
  9. 软件详细设计说明书_校导周绪龙|软件测试第五篇——软件测试的底层思维
  10. python产生5个随机数_Python和numpy生成随机数