# [leetcode]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:

“((()))”, “(()())”, “(())()”, “()(())”, “()()()”

public class Solution {
private void dfs(ArrayList<String> result,int n,String tmp,int size){//result:存储结果;n:剩余的(个数;tmp存储的临时字符串;size:当前栈中的个数（隐式的栈，而且栈中全是'(')
int i;
if(n==0){
for(i = 0;i<size;i++)
tmp+=")";
return;
}
dfs(result, n-1, tmp+"(", size+1);
if(size>0)
dfs(result, n, tmp+")", size-1);
}
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> result = new ArrayList<String>();
if(n<=0)
return result;
dfs(result, n-1, "(",1);
return result;
}
}

#### leetcode之 Generate Parentheses

2013-10-26 23:19:18

#### LeetCode 22 — Generate Parentheses（C++ Java Python）

2014-04-17 11:11:41

#### LeetCode 22：Generate Parentheses的递归，回溯两种解法

2015-08-24 11:29:56

#### leetcode-java-22. Generate Parentheses

2016-06-09 21:28:09

#### LeetCode 22 Generate Parentheses (C,C++,Java,Python)

2015-05-10 21:11:15

#### 【LeetCode】Generate Parentheses 解题报告

2014-10-16 15:26:03

#### (Java)LeetCode-22. Generate Parentheses

2016-06-01 16:19:45

#### leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

2015-07-06 22:11:08

#### LeetCode Generate Parentheses 深度分析理解

2013-11-26 08:05:53

#### LeetCode 22 Generate Parentheses（生成括号）

2015-11-10 22:41:54