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==0 && right==0)
        {
            res.push_back(ans);

        }
        else
        {
            for(int i=0;i<2;i++)
            {
                if(i==0 && left>0)
                {
                    ans.push_back(‘(‘);
                    backTrack(ans,left-1,right,res);
                    ans.pop_back();
                }
                else
                {
                    if(i == 1 && right>left && right>0)
                    {
                        ans.push_back(‘)‘);
                        backTrack(ans,left,right-1,res);
                       ans.pop_back();
                    }
                }
            }
        }
    }
    vector<string> generateParenthesis(int n) {
        vector<string> res;
        string ans;
        backTrack(ans,n,n,res);
        return res;
    }
};

原文地址:https://www.cnblogs.com/tornado549/p/9990169.html

时间: 2024-11-09 00:00:37

22. Generate Parentheses C++回溯法的相关文章

No.22 Generate Parentheses

No.22 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: "((()))", "(()())", "(())()", "()(())",

22. Generate Parentheses(js)

22. 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: [ "((()))", "(()())", "(())()", "()(())",

22. Generate Parentheses dfs填表

22. Generate Parentheses Medium 2421147FavoriteShare 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: [ "((()))", "(()())", "(())(

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

一天一道LeetCode (一)题目 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: "((()))", "(()())", "(())()", "()(())", "(

[Leetcode][Python]22: Generate Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 22: Generate Parentheseshttps://oj.leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For exampl

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

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: "((()))", "(()())", "(())()", "()(())", "

22. 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: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 思路:

【leetcode】22. Generate Parentheses

题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 解题分析: 这类题一般都要用递归的方法来解决.需要设两个集合类分别存储待匹配的(,)的个数. 这里需要明白一点:当待匹配的(的个数永远不小于待匹配的)的个数时只能匹配(,否则会导致错误.(可以自己在纸上试一下就好理解了),其余情况可以考虑匹配( 和)两种情况下可能的结果. 具体代

22. Generate Parentheses (backTracking)

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: "((()))", "(()())", "(())()", "()(())", "()()()" /** * R