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. Clarke Award for Imagination in Service to Society刘慈欣演讲

    刘慈欣不无批评地写道(http://cn.chinadaily.com.cn/2018-11/12/content_37243853.htm): 科幻的想象力由克拉克的广阔和深远,变成赛博朋克的狭窄和 ...

  2. 聊聊Python ctypes 模块(转载)

    https://zhuanlan.zhihu.com/p/20152309?columnSlug=python-dev 作者:Jerry Jho链接:https://zhuanlan.zhihu.co ...

  3. React 同构开发(一)

    为什么要做同构 要回答这个问题,首先要问什么是同构.所谓同构,顾名思义就是同一套代码,既可以运行在客户端(浏览器),又可以运行在服务器端(node). 我们知道,在前端的开发过程中,我们一般都会有一个 ...

  4. Python ImportError: No module named 'requests'解决方法

    前言:最近在学习python,安装了python3.5的环境后,在网上下载了一个python文件运行的时候,提示ImportError: No module named 'requests'(找不到r ...

  5. Django级联删除的选项

    Django级联删除的选项 Django模型中的on_delete属性具有如下选项: CASCADE 级联删除,也就是被引用的实体被删除后,相关的记录信息都会被删除. PROTECT 阻止删除被引用的 ...

  6. solr6.6教程-基础环境搭建(一)

    目前网上关于solr6.+的安装教程很少,有些6.0之前的教程在应用到6.+的版本中出现很多的问题,所以特别整理出来这一片文章,希望能给各位码农一些帮助! 很少写些文章,如有不对的地方,还希望多多指导 ...

  7. Memcached 查询stats及各项状态解释

    一.两个最常用状态查询(掌握第一个就完全OK了) 1)查看状态:printf “stats\r\n” |nc 127.0.0.1 11211      2)模拟top命令查看状态:watch “ech ...

  8. sql语句将数字转为汉字展示

    select [表字段Name] , ( case [表字段OrderState] when 1 then '已核销' when 2 then '确认前的移动端取消'when 3 then '已完成' ...

  9. 微信小程序开发框架整理

    目前除了原生的微信小程序开发外,各大厂商陆续造了自己的开发框架,现整理如下: WePY 腾讯官方开源的小程序组件化开发框架,目前有15K+Star ,一直在更新着,社区活跃,掉坑能快速的找到方法爬出来 ...

  10. asp.net Core2.1连接到Mysql 数据库

    1.首先,安装相关插件 在nuget下安装 1.Pomelo.EntityFrameworkCore.MySql 2.MySql.Data.EntityFrameworkCore 都要是2.1 < ...