【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 - 自定义异常(尚学堂第六章异常机制作业判断三角形)
写一个方法void isTriangle(int a,int b,int c),判断三个参数是否能构成一个三角形, 如果不能则抛出异常IllegalArgumentException,显示异常信息 “ ...
- jconsole 和jvisualVM 监控远程 spring boot程序
监控java 程序 增加启动参数 java \ -Djava.rmi.server.hostname=192.168.2.39 \ -Dcom.sun.management.jmxremote \- ...
- signed char型内存位bit表示
signed char型内存 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9 ...
- jQuery插件的开发(一)
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- Linux学习(1)
Linux操作系统核心"Kernel",位于操作系统底层,是连接Shell.KDE.应用和硬件的接口,核心必须支持的管理事物: 1)系统调用接口(System Call Inter ...
- uni-app 页面配置和跳转(一)转
今天看Dcloud官网更新了个uni-app,据说一套代码三端发布(Android,iOS,微信小程序),果断一试. uni.navigateTo(OBJECT) 保留当前页面,跳转到应用内的某个页面 ...
- 【vm安装vmtools】
使用sudo ./安装命令 对vmware-tools-distrib文件夹里面vmware-install.pl文件夹进行安装 sudo ./vmware-install.pl
- centos下如何停止ping命令
ctrl + c 或者 Ctrl + d(好像不行) man ping
- python os.popen 乱码问题
os.popen('ipconfig') 命令返回的结果在调试时乱码了: output1 = os.popen('ipconfig') o1=output1.read() 我猜这里输出的内容要和控制台 ...
- lintcode 题目记录3
Expression Expand Word Break II Partition Equal Subset Sum Expression Expand 字符串展开问题,按照[]前的数字展开字符 ...