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、如果左括号 '(' 数量少于n,则string末尾插入 '(';

  2、如果右括号数<左括号数,则string末尾插入 ')';

解题步骤:

1、主程序新建一个用于保存各种解的数组,并调用回溯函数;

2、回溯函数,输入为待填充的解空间lst、某一个解result、当前已填充的左括号数left,当前已填充的右括号数right,题目要求的括号数n

  (1)当result长度为n*2时,即已经完成n个括号的填充,则将result放入lst中,并结束递归;

  (2)否则,如果left<n,插入一个‘(’,继续下一层递归;

  (3)如果right<left,插入一个‘)’,继续下一层递归;

代码:

  1. class Solution {
  2. public:
  3. vector<string> generateParenthesis(int n) {
  4. vector<string> lst;
  5. Backtracking(lst, "", , , n);
  6. return lst;
  7. }
  8.  
  9. void Backtracking(vector<string> &lst, string result, int left, int right, int n) {
  10. if (result.size() == n * ) {
  11. lst.push_back(result);
  12. return;
  13. }
  14.  
  15. if (left < n)
  16. Backtracking(lst, result + '(', left+, right, n);
  17.  
  18. if (right < left)
  19. Backtracking(lst, result + ')', left, right+, n);
  20. }
  21. };

另:

//如何使用决策树实现,使叶子成为各种分配方式的集合?

【Leetcode】【Medium】Generate Parentheses的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

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

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

  6. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  7. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. 【LeetCode每天一题】Valid Parentheses(有效的括弧)

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

  10. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. nginx, flask, wsgi

    原来自己还没搞懂这些. 首先post一个观点: nginx应该是没解析任何东西,就判断是不是http请求,然后转发?或者判断是不是tcp请求,然后转发. 所以给了python后台就可以用wsgi解包. ...

  2. MySQL比较运算符的子查询

    使用比较运算符的子查询 =.>.<.>=.<=.<>.!=.<=> 语法结构 operand comparison_operator subquery ...

  3. Fiddler模拟发送post请求

    fiddler在进行接口测试时,会模拟post请求,发送不同的请求参数,返回不同的结果,今天我们就来分享一下,怎么用Fiddler工具模拟post请求: 打开Fiddler工具,在右侧点击“compo ...

  4. spring自定义标签之 规范定义XSD

    引言: spring的配置文件中,一切的标签都是spring定义好的.<bean/>等等,有了定义的规范,才能让用户填写的正常可用.想写自定义标签,但首先需要了解XML Schema De ...

  5. SVN命令行怎么用?--转百度知道

    http://zhidao.baidu.com/link?url=uPWXURahp5KzdXbgrGTb9-r-abGaNC-J7dkhFkMhf062OJ1jeCM5wpBCgDR7bDg8uFr ...

  6. Delphi下OpenGL2d绘图(01)-初始化

    一.前言: Delphi默认支持OpenGl,可以uses OpenGL单元进行引用,便可以使用OpenGL的函数.OpenGl是跨平台的,而且Windows很早就支持并集成在系统中,存在于syste ...

  7. 【读书笔记】读《编写可维护的JavaScript》 - 编程实践(第二部分)

    本书的第二个部分总结了有关编程实践相关的内容,每一个章节都非常不错,捡取了其中5个章节的内容.对大家组织高维护性的代码具有辅导作用. 5个章节如下—— 一.UI层的松耦合 二.避免使用全局变量 三.事 ...

  8. Struts2 学习(二)

    一.Struts2 配置文件 1.配置多个配置文件 在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿. 为了避免struts.xm ...

  9. 前端(五):JavaScript面向对象之内建对象

    一.数据类型 js中数据类型分为两种,原始数据累次能够和引用数据类型. 1.原始数据类型 Undefined.Null.Boolean.Number.String是js中五种原始数据类型(primit ...

  10. mysql的with rollup

    无意间发现了mysql的with rollup函数(用在group by 函数后面) 测试 1. SELECT NAME,DATE,score FROM stu 结果是 2. SELECT NAME, ...