Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
Created March 12, 2013 21:24
Show Gist options
  • Save guolinaileen/5147166 to your computer and use it in GitHub Desktop.
Save guolinaileen/5147166 to your computer and use it in GitHub Desktop.
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
return permutation( num, num.length-1);
}
ArrayList<ArrayList<Integer>> permutation(int [] num, int end)
{
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>> ();
if(end<0)
{
result.add(new ArrayList<Integer>());
return result;
}
int rest=num[end];
ArrayList<ArrayList<Integer>> subResult=permutation(num, end-1);
for(ArrayList<Integer> list: subResult)
{
int size=list.size();
for(int i=0; i<=size; i++)
{
list.add(i, rest);
result.add((ArrayList<Integer>)list.clone());
list.remove(i);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment