1 Star 0 Fork 332

[]~( ̄ ̄)~* / leetcode

forked from doocs / leetcode 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 3.38 KB
一键复制 编辑 原始数据 按行查看 历史
ylb 提交于 2021-12-24 22:51 . style: format code and docs (#645)

面试题 08.09. 括号

English Version

题目描述

括号。设计一种算法,打印n对括号的所有合法的(例如,开闭一一对应)组合。

说明:解集不能包含重复的子集。

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

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

解法

深度优先搜索 DFS。

Python3

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        ans = []

        def dfs(left, right, t):
            if left == n and right == n:
                ans.append(t)
                return
            if left < n:
                dfs(left + 1, right, t + '(')
            if right < left:
                dfs(left, right + 1, t + ')')

        dfs(0, 0, '')
        return ans

Java

class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> ans = new ArrayList<>();
        dfs(0, 0, n, "", ans);
        return ans;
    }

    private void dfs(int left, int right, int n, String t, List<String> ans) {
        if (left == n && right == n) {
            ans.add(t);
            return;
        }
        if (left < n) {
            dfs(left + 1, right, n, t + "(", ans);
        }
        if (right < left) {
            dfs(left, right + 1, n, t + ")", ans);
        }
    }
}

TypeScript

function generateParenthesis(n: number): string[] {
    let ans = [];
    dfs(0, 0, n, "", ans);
    return ans;
}

function dfs(left: number, right: number, n: number, t: string, ans: string[]) {
    if (left == n && right == n) {
        ans.push(t);
        return;
    }
    if (left < n) {
        dfs(left + 1, right, n, t + "(", ans);
    }
    if (right < left) {
        dfs(left, right + 1, n, t + ")", ans);
    }
}

C++

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> ans;
        dfs(0, 0, n, "", ans);
        return ans;
    }

    void dfs(int left, int right, int n, string t, vector<string>& ans) {
        if (left == n && right == n)
        {
            ans.push_back(t);
            return;
        }
        if (left < n) dfs(left + 1, right, n, t + "(", ans);
        if (right < left) dfs(left, right + 1, n, t + ")", ans);
    }
};

Go

func generateParenthesis(n int) []string {
	var ans []string
	dfs(0, 0, n, "", &ans)
	return ans
}

func dfs(left, right, n int, t string, ans *[]string) {
	if left == n && right == n {
		*ans = append(*ans, t)
		return
	}
	if left < n {
		dfs(left+1, right, n, t+"(", ans)
	}
	if right < left {
		dfs(left, right+1, n, t+")", ans)
	}
}

JavaScript

/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function (n) {
    let res = [];
    dfs(n, 0, 0, "", res);
    return res;
};

function dfs(n, left, right, prev, res) {
    if (left == n && right == n) {
        res.push(prev);
        return;
    }
    if (left < n) {
        dfs(n, left + 1, right, prev + "(", res);
    }
    if (right < left) {
        dfs(n, left, right + 1, prev + ")", res);
    }
}

...

Java
1
https://gitee.com/keshuimao/leetcode.git
git@gitee.com:keshuimao/leetcode.git
keshuimao
leetcode
leetcode
main

搜索帮助