[LeetCode]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:
[ “((()))”, “(()())”, “(())()”, “()(())”, “()()()”]
这道题的答案是很多种左右括号组合成的一个set,因为情况有很多种,所以常规的算法比较麻烦,所以采取递归的方法解决。算法的复杂度是O(n^2).
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> re;
recursion(re,n,0,"");
return re;
}
void recursion(vector<string> &re,int l,int r,string str){
if(l == 0 && r == 0){
re.push_back(str);
}
if(r>0) recursion(re,l,r-1,str+")");
if(l>0) recursion(re,l-1,r+1,str+"(");//用掉一个左括号(l-1),可用的有括号就多了一个(r+1)
}
};
举例n>3的时候,递归的过程如下:
"("
/ \
"((" "()"
/ \ / \
"(((" "(()" "()(" ** //星号处,没有可用的右括号
...
[LeetCode]Generate Parentheses题解的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode——Generate Parentheses
Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...
- LeetCode: Generate Parentheses [021]
[称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- LeetCode Generate Parentheses 构造括号串(DFS简单题)
题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...
- leetcode Generate Parentheses python
# 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...
- 并没有看起来那么简单leetcode Generate Parentheses
问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...
随机推荐
- ElasticSearch安装拼音插件 elasticsearch-analysis-pinyin
elasticsearch-analysis-pinyin 是 ElasticSearch的拼音插件.强大的功能支持拼音等的搜索 1.下载源代码 源码地址https://github.com/medc ...
- 为服务器设置固定IP地址
为服务器设置固定IP地址 1.获取超级管理员权限 命令:$ su - 输入root密码 2.判断哪个网卡有流量,或者确定需要设置哪个网卡的固定ip 命令:# ifconfig PS:可以查询哪些网卡有 ...
- 前后端分离——token超时刷新策略
前言 记录一下前后端分离下————token超时刷新策略! 需求场景 昨天发了一篇记录 前后端分离应用——用户信息传递 中介绍了token认证机制,跟几位群友讨论了下,有些同学有这么一个疑惑:toke ...
- 10分钟教你用Python打造微信天气预报机器人
01 前言 最近武汉的天气越来越恶劣了.动不动就下雨,所以,拥有一款好的天气预报工具,对于我们大学生来说,还真是挺重要的了.好了,自己动手,丰衣足食,我们来用Python打造一个天气预报的微信机器人吧 ...
- 基于CH340的一键下载电路
一.CH340简介 CH340 是一个 USB 总线的转接芯片,实现 USB 转串口或者 USB 转打印口.CH340是国产芯片,应用场合居多,市场占有率很高.常用的USB转串口芯片还有CP2102. ...
- Python小实验——读&写Excel文件内容
安装xlrd模块和xlwt模块 读取Excel文件了内容需要额外的模块-- \(xlrd\),在官网上可以找到下载:https://pypi.python.org/pypi/xlrd#download ...
- 图示NP, P, NP-Complete和NP-Hard问题
P问题是一类可以通过确定性图灵机(以下简称图灵机)在多项式时间(Polynomial time)内解决的问题集合. NP问题是一类可以通过非确定性图灵机( Non-deterministic Turi ...
- 牛客OI周赛8-提高组A-用水填坑
牛客OI周赛8-提高组A-用水填坑 题目 链接: https://ac.nowcoder.com/acm/contest/403/A 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制: ...
- android逆向基础:apk 反编译 重打包 重签名
apk 反编译大家都比较熟悉,这里只做一个笔记. 1 反编译 apk apktool d perfect.apk 这样就把资源文件解压缩了, classes.dex 也反编译成了 smali 文件 2 ...
- 【JVM调优系列】----CPU过高的分析与解决方案
1.首先通过top命令查询当前进程所占cpu的一个比重