【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 ...
随机推荐
- java中Map转化为bean
Map 集合类用于存储元素对(称作“键”和“值”),其中每个键映射到一个值,在java编程中会经常用到.但是当我们进行业务逻辑的处理或着操作数据库时,往往应用的是我们自己定义的的Bean或VO来传递和 ...
- centos 7编译安装mysql 5.7.17
1.进入MySQL官网下载MySQL源代码 依次点击Downloads -> Community -> MySQL Community Server 源代码1.Select Operati ...
- 加载 Firefox 配置
有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用 firebug在打开的页面上继续定位页面元素,调试起来不方便 .加载浏览器配置,需要用 FirefoxProfile(profile_d ...
- 在VM虚拟机中彻底删除Linux系统
前言:很久之前安装了Linux虚拟系统,然后用户名忘记了,想着重新安装个Ubuntu系统,就想着删除以前的系统. 删除方法如下: 1.点击打开该Linux系统. 2. 点击虚拟机的左上方“虚拟机”-& ...
- dfs.replication、dfs.replication.min/max及dfs.safemode.threshold.pct
一.参数含义 dfs.replication:设置数据块应该被复制的份数: dfs.replication.min:所规定的数据块副本的最小份数: dfs.replication.max:所规定的数据 ...
- 转 File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:
因为yum包使用python2*等开发,修该为环境修改python3之后有问题, 解决: 修改文件/usr/bin/yum./usr/libexec/urlgrabber-ext-down头中相应py ...
- 阿里云 maven 地址
http://maven.aliyun.com/nexus/content/groups/public/ 阿里云的 maven 地址
- 测试域名ping延迟脚本
#!/bin/bash if [ $# -lt 1 ]thenecho "Usage:avg file1"exit 1fiecho "================== ...
- 查询数据库的所有列信息 sys.all_columns
一.Database.sys.tables 为每个表对象返回一行,当前仅用于 sys.objects.type = U 的表对象. 列名 数据类型 说明 <继承的列> 有关此视图所继承 ...
- Java - 线程封闭
保证并发安全性的方式有三: 不共享.不可变.同步 前两种方式相对第三种要简单一些. 这一篇不说语言特性和API提供的相关同步机制,主要记录一下关于共享的一些思考. 共享(shared),可以简单地认为 ...