leetcode-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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

例如,给出 = 3,生成结果为:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

思路:DFS. 对这种列出所有结果的题目,都可以考虑用递归来解。这里字符串只有左括号和右括号两种,每种3个,我们令left=3为左括号个数,right=3为右括号个数。挡在某次递归时出现left>right,则直接返回,若出现left==right==0,就说明左右 括号都打印完了。否则,先打印左括号,left--,再打印右括号,right--.

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> res;
        dfs(n,n,"",res);
        return res;
    }
    void dfs(int left,int right,string out,vector<string> &res)
    {
        if(left > right) return;
        if(left==0 && right == 0) res.push_back(out);
        else
        {
            if(left>0) dfs(left-1,right,out+"(",res);
            if(right>0) dfs(left,right-1,out+")",res);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_21997625/article/details/85158647
今日推荐