22. Generate Parentheses C++回溯法
把左右括号剩余的次数记录下来,传入回溯函数。
判断是否得到结果的条件就是剩余括号数是否都为零。
注意判断左括号是否剩余时,加上left>0的判断条件!否则会memory limited error!
判断右括号时要加上i==1的条件,否则会出现重复的答案。
同样要注意在回溯回来后ans.pop_back()
class Solution {
public:
void backTrack(string ans, int left, int right, vector<string>& res)
{
if(left== && right==)
{
res.push_back(ans);
}
else
{
for(int i=;i<;i++)
{
if(i== && left>)
{
ans.push_back('(');
backTrack(ans,left-,right,res);
ans.pop_back();
}
else
{
if(i == && right>left && right>)
{
ans.push_back(')');
backTrack(ans,left,right-,res);
ans.pop_back();
}
}
}
}
}
vector<string> generateParenthesis(int n) {
vector<string> res;
string ans;
backTrack(ans,n,n,res);
return res;
}
};

22. Generate Parentheses C++回溯法的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
随机推荐
- ElasticSearch——日志工具
Elasticsearch: 权威指南 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.ht ...
- js 模块化规范
模块规范 CommonJS module.exports, exports 导出模块 require 加载模块, CommonJS 同步,服务端.实践者: nodejs ES6 export, exp ...
- hostname命令
hostname命令用于显示和设置系统的主机名称.环境变量HOSTNAME也保存了当前的主机名.在使用hostname命令设置主机名后,系统并不会永久保存新的主机名,重新启动机器之后还是原来的主机名. ...
- Python 学习笔记 多进程 multiprocessing--转载
本文链接地址 http://quqiuzhu.com/2016/python-multiprocessing/ Python 解释器有一个全局解释器锁(PIL),导致每个 Python 进程中最多同时 ...
- HTML元素 绑定href属性
给<li>标签绑定href属性 <ul class="honor-list clearfix"> <li style="cursor:poi ...
- 【BZOJ】1875: [SDOI2009]HH去散步
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1875 注意的是路径不可以重复,所以这题把边看成点.每一条无向边拆成两条有向边. 令${F[ ...
- hdu 6041 I Curse Myself 无向图找环+优先队列
I Curse Myself Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- SHA-256 加密原理
网络中传输敏感信息的时候通常会对字符串做加密解密处理 SHA-256 加密原理
- Microsoft Active Directory(LDAP)连接常见错误代码
接下来显示的认证错误类似于这样: "The exception is [ LDAP: error code 49 - 80090308: LdapErr: DSID-0Cxxxxxx, co ...
- Windows.环境变量(设置)
ZC: 我的示例代码(Delphi):http://www.cnblogs.com/CodeSkill/p/8341464.html 1.资料: 如何用代码设置环境变量?-CSDN论坛.html(ht ...