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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]思路:  如果n=1,那么最终结果都是2个符号,如果n=3,最终结果都是6个符号,所以只需要开一个2*n+1的字符数组枚举左右括号即可,第2*n个字符用于保存‘\0‘(字符串结束标记),这样的时间复杂度是2^n次方。由于这样枚举出来的结果中有许多不合法,所以可以进行剪枝处理。    1)第一个位置一定填充左括号    2)如果当前的右括号个数大于左括号一定是错误的    3)如果左括号个数大于一半,则一定不合法
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

void solve(vector<string>&ans,int n,int i,int left,int right,char ch[])
{
    if(left>n)
        return;
    if(i == 2*n)
    {
        ans.push_back(ch);
        return;
    }
    ch[i]=‘(‘;
    solve(ans,n,i+1,left+1,right,ch);
    if(right<left)
    {
        ch[i]=‘)‘;
        solve(ans,n,i+1,left,right+1,ch);
    }
}

vector<string> generateParenthesis(int n)
{
    vector<string>ans;
    if(n==0)
        return ans;
    char ch[2*n+1];
    ch[0]=‘(‘;
    ch[2*n]=‘\0‘;
    solve(ans,n,1,1,0,ch);
    return ans;
}

int main()
{
    vector<string>ans;
    ans=generateParenthesis(1);
    for(int i=0;i<ans.size();i++){
        cout<<ans[i]<<endl;
    }
    return 0;
}



原文地址:https://www.cnblogs.com/zhanghaijie/p/10504913.html

时间: 2024-11-07 22:14:56

22. Generate Parentheses dfs填表的相关文章

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

[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 (String; dfs)

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: "((()))", "(()())", "(())()", "()(())", "()()()" 思路:两个df

22. Generate Parentheses——本质:树,DFS求解可能的path

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

【leetcode】22. Generate Parentheses

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

22. Generate Parentheses - Medium

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

#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: "((()))", "(()())", "(())()", "()(())", "()()()" 本题是括号匹配