题目

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个左括号,在递归调用的时候,在能传递到最底层的共用字符串中先添加”(“,然后left-1,递归调用就可以。

那什么时候添加右括号呢?当左括号个数大于右括号的个数时添加右括号。

那我们是先添加右括号还是先添加左括号呢?对于这个问题,认真想想其实是无所谓的,只会影响在vector中最后字符串的顺序而已。

难度主要在以下几个方面:

1.想到二叉树,想到递归实现。

2.递归函数的参数形式要考虑清楚才可以。

AC代码

class Solution {
public:
vector<string> generateParenthesis(int n) {
if (n == 0)
return vector<string>(); vector<string > ret; dfs(ret, "", n, n); return ret;
} //利用二叉树递归思想
void dfs(vector<string> &ret, string tmp, int left, int right)
{
if (0 == left && 0 == right)
{
ret.push_back(tmp);
return;
}
else if (left > 0)
dfs(ret, tmp + '(', left - 1, right); if (left < right)
dfs(ret, tmp + ')', left, right - 1);
} };

GitHub测试程序源码

LeetCode(22)Generate Parentheses的更多相关文章

  1. LeetCode(49)-Valid Parentheses

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  2. LeetCode(22):括号生成

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

  3. LeetCode(20)Valid Parentheses

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  4. Leetcode(22)-括号生成

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

  5. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  6. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(22)-权限管理系统-模块导航制作

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(22)-权限管理系统-模块导航制作 最近比较忙,系统难度独步增加,文章的发布速度明显比以前慢了. 由于我们 ...

  8. Windows Phone开发(22):启动器与选择器之BingMapsDirectionsTask

    原文:Windows Phone开发(22):启动器与选择器之BingMapsDirectionsTask 从今天开发始,我们又开始新的征程,接下来的课程我们要熟悉一下启动器和选择器,其实二者是一样的 ...

  9. Java设计模式(22)命令模式(Command模式)

    Command模式是最让我疑惑的一个模式,我在阅读了很多代码后,才感觉隐约掌握其大概原理,我认为理解设计模式最主要是掌握起原理构造,这样才对自己实际编程有指导作用.Command模式实际上不是个很具体 ...

随机推荐

  1. 编译安装Apache:出现错误configure: error: mod_deflate

    在进行编译安装Apache时,出现如下错误 checking whether to enable mod_deflate... configure: error: mod_deflate has be ...

  2. NowCoder小定律

    题目:https://www.nowcoder.com/pat/2/problem/259 #include <cstdio> #include <cstring> #incl ...

  3. kettle 导入xml 资源文件

    Repository | ExploreRight click the root node of the repositorySelect Import objects from an XML fil ...

  4. javascript简单的表单验证

    <html> <head> <title>用户登录</title> <script language="javascript" ...

  5. canvas 平移&缩放

    1.平移 canvas其实只是一个包装器,真正起着重要作用的部分是2D渲染上下文,这才是我们真正绘制图形的地方. 然而2D渲染上下文是一种基于屏幕的标准绘制平台.它采用屏幕的笛卡尔坐标系统,以左上角( ...

  6. javascript 找出数字数组中最大的数

    找出数字数组中最大的数 var Match = (function(){ var arr = null; var len = 0; return { max:function(arr,len){ ar ...

  7. ES6学习笔记(4)----正则的扩展

    参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ 正则的扩展 ES6新增的正则表达式修饰符 u修饰符a.能够更准确地匹配unicode大于\uFF ...

  8. CCF|中间数|Java

    import java.util.*; public class tyt { public static void main(String[] args) { Scanner in = new Sca ...

  9. 关于php的问题

    $polling_items = db_fetch_assoc("SELECT * FROM poller_item WHERE rrd_next_step<=0 ORDER by h ...

  10. Javaweb学习笔记9—过滤器

      今天来讲javaweb的第9阶段学习.   过滤器,我在本次的思维导图中将过滤器和监听器放在一起总结了,监听器比较简单就不单独写了.   老规矩,首先先用一张思维导图来展现今天的博客内容.     ...