【Leetcode】【Medium】Generate Parentheses
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【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 ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode每天一题】Valid Parentheses(有效的括弧)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【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 ...
随机推荐
- Git删除和恢复文件
删除文件: 如果你在本地删除了一个文件但是没有提交到版本库,这时你用 $ git status命令会看到提示: 如果需要从版本库中删除该文件,则需要用 $ git rm 和 $ git commit ...
- 在bootstrap modal 中加载百度地图的信息窗口失效解决方法
这个问题其实很傻,解决方法没有任何技术含量,只是记录下工作中发生的事. 前阵子给一个汽车集团客户做了一个经销商查询系统,其中一个功能是使用地图标注经销商店面地址,并且实现导航功能. 页面演示地址:ht ...
- Oracle 如何修改列的数据类型
链接:http://www.cnblogs.com/david-zhang-index/archive/2012/04/10/2441015.html 对字段操作 操作方法 更新字段名 alter t ...
- line-height详解
line-height详解 要说line-height就必须要知道这几个概念了: 顶线.中线.基线.底线. 这也就是在vertical-align中可能用到的top,middle,baseline和b ...
- spring、springmvc和mybatis整合(xml方式)
今天搭建一个基于xml的ssm整合demo.话不多说,直接上代码. 我的开发环境如下: web服务器:tomcat8 开发工具:STS JDK版本:1.8 项目构建工具:maven 1.pom.xml ...
- 【wordpress】wordpress初探
接下来,开始wordpress之旅! 访问wordpress文件夹下的index.php 点击现在就开始. 这里要求我们输入数据库名. 所以先去mysql中新建一个wordpress库 create ...
- Python获取当前路径下的配置文件
Python获取当前路径下的配置文件 有的时候想读取当前目录下的一个配置文件.其采用的办法是: import os # 获取当前路径 curr_dir = os.path.dirname(os.pat ...
- CentOS7下Django安装
Django安装介绍 安装环境: CentOS7 安装Django比较简单,但需要安装其依赖的东西,还是需要一定时间的.我使用的环境是CentOS Linux release 7.3.1611. 内核 ...
- 【c++】常识易混提示
1. struct 和 class 唯一的区别:默认的成员保护级别和默认的派生保护级别不同(前者为public,后者为private). 2. int *p = new int[23]; de ...
- bat执行java程序 good
start.bat set MY_HOME=%~dp0 set JMS_BINDING_PATH=%MY_HOME%..\binds set JAVA_HOME=C:\Program Files\J ...