Skip to content

Instantly share code, notes, and snippets.

@onewaterdrop
Last active January 2, 2016 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onewaterdrop/8277813 to your computer and use it in GitHub Desktop.
Save onewaterdrop/8277813 to your computer and use it in GitHub Desktop.
Sharing samples of leetcode solutions and awesome codes
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