把左右括号剩余的次数记录下来,传入回溯函数。

判断是否得到结果的条件就是剩余括号数是否都为零。

注意判断左括号是否剩余时,加上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++回溯法的更多相关文章

  1. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  2. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  3. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  4. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  5. 【一天一道LeetCode】#22. Generate Parentheses

    一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...

  6. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  7. 22.Generate Parentheses[M]括号生成

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  9. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

随机推荐

  1. s*s*r备用

    遇见的问题 突然打不开 ip被墙 能ping 但是不能ssh 参考https://www.vultrcn.com/6.html

  2. C#Listview添加数据,选中最后一行,滚屏

    this.listView.Items.Add(lvi); this.listView.EnsureVisible(this.listView.Items.Count - 1); this.listV ...

  3. JAVA读取CSV文件到MySQL数据库中

    maven项目pom配置: <dependency> <groupId>net.sourceforge.javacsv</groupId> <artifact ...

  4. 烽火HG220G-U E00L2.03M2000光猫改桥接教程

    烽火HG220G-U E00L2.03M2000光猫改桥接教程 P.S. 此教程同样适用于HG221G/HG260G-U/HG261G.(2016.12) 随着北京联通从原有的ONU升级到HGU之后, ...

  5. go 多态

    demo1 // Sample program to show how polymorphic behavior with interfaces. package main import ( &quo ...

  6. base64 加密原理 解密原理

    假设需要加密的字符串是Jasmine 具体转换步骤: 第一步 将待转换的字符串转为一个个字符第二步 计算每一个字符对应的ASCII码十进制第三步 计算出十进制对应的二进制,若不足8位,在前面添加0进行 ...

  7. js全选 反选

    // 全选 反选 allChoose: function (o) { var obj = $.extend(true, { id: "#id", name: "name& ...

  8. Jtest的简单使用

    Jtest主要用于快速测试自己的代码是否正确 条件,导入相应的Jtest包 @Test    public void test() {        System.out.println(" ...

  9. [转][osg][QT]osg与QT界面结合的简单例子

    //QT += core gui opengl //LIBS += -losgViewer -losgDB -losgUtil -losg -lOpenThreads -losgGA -losgQt ...

  10. WebAPI使用Token进行验证

    1.需要用到的包  可以先敲代码   发现没有包在添加 2.在项目根目录下Web层添加“Startup”类   这个是Token的配置 3.在WebAPI层添加WebApiConfig类  也是Tok ...