Java for LeetCode 022 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=2和n=3的情况可以知道,只要在n=2的String 开头、末尾、‘(‘ 插入“()”即可,注意防止重复。

JAVA实现如下:

static public List<String> generateParenthesis(int n) {
		HashSet<String> set = new HashSet<String>();
		if (n == 1)
			set.add("()");
		if (n > 1) {
			ArrayList<String> lastList = new ArrayList<String>(
					generateParenthesis(n - 1));
			StringBuilder sb = new StringBuilder();
			for (String string : lastList) {
				sb = new StringBuilder(string);
				set.add(sb.toString() + "()");
				set.add("()" + sb.toString());
				for (int i = 0; i < sb.length() - 1; i++) {
					if (sb.charAt(i) == ‘(‘) {
						sb.insert(i + 1, "()");
						set.add(sb.toString());
					}
					sb = new StringBuilder(string);
				}
			}
		}
		return new ArrayList<String>(set);
	}

解题思路二:

通过观察可以发现,任何符合条件的Parenthesis(n)总是可以分解为(generateParenthesis(k))+generateParenthesis(n-1-k),同时由于后半部分generateParenthesis(n-1-k)的唯一性,这种算法不会产生重复的元素,因此采用DFS进行一次遍历即可,这种算法更高效!JAVA实现如下:

static public List<String> generateParenthesis(int n) {
		List<String> list = new ArrayList<String>(), leftList, rightList;
		if (n == 0)
			list.add("");// 很关键,不能删除
		if (n == 1)
			list.add("()");
		else {
			for (int i = 0; i < n; i++) {
				leftList = generateParenthesis(i);
				rightList = generateParenthesis(n - i - 1);
				for (String leftPart:leftList)
					for (String rightPart:rightList)
						list.add("(" + leftPart + ")" + rightPart);
			}
		}
		return list;
	}
时间: 2024-11-08 03:43:03

Java for LeetCode 022 Generate Parentheses的相关文章

[LeetCode] 022. Generate Parentheses (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 022.Generate_Parentheses (Medium) 链接: 题目:https://oj.leetcode.com/problems/generate-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 产生有 n 对括号的所有有

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

【leetcode】Generate Parentheses

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

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] 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,

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

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

【LeetCode】Generate Parentheses (2 solutions)

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】Generate Parentheses in 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: "((()))", "(()())", "(())()", "()(())", "()()()" 用dfs思想做