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:

"((()))", "(()())", "(())()", "()(())", "()()()"

暴力思路:其实就是组合,n=3,则有6个位置,每个位置可以插入‘(‘,或者‘)‘,当n=3时,就有64中可能,只需对每一种可能作必要的筛选即可。

代码:

class Solution {
private:
    char parenthesis[2];
    vector<string> res;
    int num;
public:
    void dfs(int dep,string temp){
        if(dep==num){
            stack<char> s;
            for (int i=0;i<temp.size();++i)
            {
                if(s.empty())
                    s.push(temp[i]);
                else if(!s.empty()&&temp[i]==‘)‘){
                    if(s.top()==‘(‘)
                        s.pop();
                }else{
                    s.push(temp[i]);
                }
            }
            if(!s.empty()) return;
            res.push_back(temp);
            return;
        }
        for (int i=0;i<2;++i)
        {
            temp.push_back(parenthesis[i]);
            dfs(dep+1,temp);
            temp.pop_back();
        }
        return;
    }
    vector<string> generateParenthesis(int n) {
        parenthesis[0]=‘(‘;
        parenthesis[1]=‘)‘;
        num=n*2;
        string temp="";
        dfs(0,temp);
        return res;
    }
};
时间: 2024-10-21 12:52:07

Generate Parentheses(组合,回溯)的相关文章

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>&

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

Generate Parentheses java实现

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: Generate Parentheses [021]

[题目] 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: Generate Parentheses 解题报告

Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

Generate Parentheses leetcode java

题目: 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 暴力 Generate Parentheses

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Generate Parentheses Total Accepted: 11470 Total Submissions: 37301 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

[LeetCode][JavaScript]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: "((()))", "(()())", "(())()", "()(())", "