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,插入一个‘)’,继续下一层递归;

代码:

 class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> lst;
Backtracking(lst, "", , , n);
return lst;
} void Backtracking(vector<string> &lst, string result, int left, int right, int n) {
if (result.size() == n * ) {
lst.push_back(result);
return;
} if (left < n)
Backtracking(lst, result + '(', left+, right, n); if (right < left)
Backtracking(lst, result + ')', left, right+, n);
}
};

另:

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

【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. Macaca 等待机制

    看代码注释todo 写博客 服务写脚本开吧 , 因为窗口太多,  不知道要去哪关闭服务 开的话无所谓 , 哪里都能开 要确认是否有开 , 直接跑代码 下面的要先过 别人的环境 工具软件自己的问题 不支 ...

  2. git 学习之基本操作

    之前的帖子已经讲述了什么是 Git 的仓库,并且添加了文件到 Git 的仓库,这里我们来学习下一些简单的操作. status 和 diff  之前我们已经提交了了一个 testFile.txt 的文件 ...

  3. linux 查看端口,开启新端口

    一.查看端口被占用命令 1.lsof -i:端口号 2.netstat -tunlp|grep 端口号 3.netstat -anp 查看哪些端口被打开 上面命令是查看端口被进程占用的情况 二.开启新 ...

  4. Java线程池——ThreadPoolExecutor的使用

    1 线程池的创建 ThreadPoolExecutor有以下四个构造方法 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long  ...

  5. Java中break、continue及标签等跳转语句的使用[上]

    java 中跳转语句使用break.continue和标签,各自或组合完成相应的功能. 今天做题时遇到关于标签命名规范,顺便将跳转语句语法都看了一遍,很有收获. 在<Java编程思想>一书 ...

  6. Splunk数据处理

    0.提要 本篇主要从技术层面针对Splunk Enterprise中关于数据处理的概念.过程与部件进行了概要性总结. 1.数据管理基本概念 索引(index):Splunk用于存储事件的数据仓库: 索 ...

  7. 第六章使用java实现面向对象-集合框架

    一:接口:即表示集合的抽象数据类型. 实现:即集合框架中接口的实现. 算法:在一个实现了某个集合框架中的接口的对象身上完成某种有用的计算的方法,例如查找. 排序等. Collection 接口存储一组 ...

  8. JavaScript - 表单提交前预览图片

    其实这东西网上到处都是,但并不完整. 正好我也遇到了这个问题,不仅仅是预览,还需要得到图片的属性. 于是东凑西凑整理出一个完整的版本,并根据个人的理解加上了一点点说明. 首先做一些准备工作,HTML方 ...

  9. python4

    列表的常用操作     创建列表         1.创建空列表             列表变量 = []         2.创建单个数据的列表             列表变量 = [值]    ...

  10. 前端小结(4)---- 页面加载loding....

    /*正在加载*/ function showLoading(elem) { var html = '<div class="loading"><div id=&q ...