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 ...
随机推荐
- C++ getline判断空行
C++中getline用于逐行读取字符, 格式 getline(字符串,字符数) 将该行“字符数”个的字符读入“字符串” 如何判断所读是否为空行呢? strlen(字符串)==0就是空行
- jmeter命令行模式运行,实时获取压测结果
jmeter命令行模式运行,实时获取压测结果 jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式: sh jmeter.sh -n -t my-script. ...
- JS实现ul,li排序效果
<!DOCTYPE html> <html> <head> <title>js列表排序</title> <meta charset=& ...
- Python 网页解析器
Python 有几种网页解析器? 1. 正则表达式 2.html.parser (Python自动) 3.BeautifulSoup(第三方)(功能比较强大) 是一个HTML/XML的解析器 4.lx ...
- 一行css解决图片统一大小后的拉伸问题(被冷漠的object-fit)
一.先来个实战 1. 测试案例 需求: 要求表情库里所有表情包大小都固定 实际效果: 由于图片原始大小都不一样,强行设定大小值会导致拉伸,如果不设定大小则参差不齐.例如: //html <bod ...
- command not found shell returned 127
在 vim 修改某个文件后,退出时,报了如此一个错误.日志如下: 并不是什么大问题,只是在刚入坑 ssh 时,真的被人代入坑里了. # 强制退出并保存 :wq! 不是 :!wq,不知道有没有有缘的小伙 ...
- MySql登陆密码忘记了怎么办?MySQL重置root密码方法
本文主要介绍Windows和Linux系统下忘记密码重置root密码的方法,需要的朋友可以参考下. MySQL有时候忘记了root密码是一件伤感的事.这里提供Windows 和 Linux 下的密码重 ...
- char,String,int类型互转
1.ascci码对应转换 字符,对应的ascii(其实是UTF-16)码: char c='a'; int k=(int) c; 结果k=97 数字,对应的字符: int k=9 ...
- STL_map.VC6简单使用例子
1. #include <windows.h> //使用map时会出现如下警告:主要意思是 identifier was truncated to '255' characters in ...
- Hadoop如何将TB级大文件的上传性能优化上百倍?
这篇文章,我们来看看,Hadoop的HDFS分布式文件系统的文件上传的性能优化. 首先,我们还是通过一张图来回顾一下文件上传的大概的原理. 由上图所示,文件上传的原理,其实说出来也简单. 比如有个TB ...