Last active
January 2, 2016 08:39
-
-
Save onewaterdrop/8277813 to your computer and use it in GitHub Desktop.
Sharing samples of leetcode solutions and awesome codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package generating.paranthesis; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
public class Solution { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
System.out.println(generateParenthesis(5).toString()); | |
} | |
public static ArrayList<String> generateParenthesis(int n) { | |
return brackets(n, 0, ""); | |
} | |
public static ArrayList<String> brackets(int openStock, int closeStock, | |
String s) { | |
ArrayList<String> temp = new ArrayList<String>(); | |
if (openStock == 0 && closeStock == 0) { | |
temp.add(s); | |
} | |
if (openStock > 0) { | |
temp.addAll(brackets(openStock - 1, closeStock + 1, s + "(")); | |
} | |
if (closeStock > 0) { | |
temp.addAll(brackets(openStock, closeStock - 1, s + ")")); | |
} | |
return temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment