Search This Blog

Tuesday, December 11, 2012

LeetCode: Permutations

Permutations
Given a collection of numbers, return all possible permutations.
For example, [1,2,3] have the following permutations: [1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

public class Solution {
    public ArrayList<ArrayList<Integer>> permute(int[] num) {
        // Start typing your Java solution below
        // DO NOT write main() function
        
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        res.add(new ArrayList<Integer>());
        
        for(int i=0;i<num.length;i++){
            ArrayList<ArrayList<Integer>> cur = new  ArrayList<ArrayList<Integer>>();
            for(ArrayList<Integer> temp:res){
                for(int j=0;j<temp.size()+1;j++){
                    temp.add(j,num[i]);
                    ArrayList<Integer> temp1= new ArrayList<Integer>(temp);
                    cur.add(temp1);
                    temp.remove(j);
                }
            }
            res = new ArrayList<ArrayList<Integer>>(cur);
        }
        return res;
    }
}

No comments: