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 ...
随机推荐
- 洛谷P2637第一次,第二次,成交! 模拟?DP?
今天水来一天,就贴道水吧.. 原题>>https://www.luogu.org/problem/show?pid=2637<< 题目描述 因为奶牛们的节食运动(奶牛还节食?) ...
- C#——LINQ语句
委托: //delegate 返回值 委托名(参数); //委托不能在方法中定义 ////实例化委托,并赋值 //委托名 实例名 = new 委托名(函数名).lambda表达式; //使用委托实例, ...
- IDEA @Autowired 出现红色下划线 报红
例如: 解决方法:
- Django中ORM系统多表数据操作
一,多表操作之增删改查 1.在seting.py文件中配置数据库连接信息 2.创建数据库关联关系models.py from django.db import models # Create your ...
- 2.0 vue内置指令与自定义指令
1.1 常用内置指令 1) v:text : 更新元素的 textContent 2) v-html : 更新元素的 innerHTML 3) v-if : 如果为 true, 当前标签才会输出到页 ...
- Fiddler 手机抓包介绍
直接打开tools -> Options 进行设置 点击OK,在这里代理就设置完成,一定要重启软件配置才生效,下面是手机端的设置. 手机端代理设置以三星S4为例子,1.如下图真机三星S4设置:找 ...
- Unity--- 纹理设置属性 alphaIsTransparency
官方的解释: 意思就是没什么实际效果,只是用做显示用. 参考:https://docs.unity3d.com/ScriptReference/Texture2D-alphaIsTransparenc ...
- sublime Text如何取消两栏窗口?
在菜单栏里的 View->LayOut->Single,也可以用快捷键 Alt+Shift+1.如图所示.(亲测可用) &lt;img src="https:// ...
- Java中类似C#中Task.wait()的类CountDownLatch
当主线程开辟多个子线程,而又需要这些子线程都执行完成后再执行主线程后续的操作,在C#中可以通过Task的wait方法来实现,然而在Java中也有类型的类CountDownLatch,具体用法如下: p ...
- 确认OHS版本的方法
还是 opatch lsinventory 好用 C:\Oracle\Middleware\ohs\OPatch>opatch lsinventory Oracle Interim Patch ...