My solution in Java, instead of backtracking , I use generating


  • 0
    B
    public List<List<Integer>> permute(int[] num) {
        ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>();
        ret.add(new ArrayList<Integer>());
        
        for(int i = 0,j,k ;i < num.length ; i ++){
            int ret_size = ret.size();
            for( j = 0; j < ret_size; j++){
                List<Integer> l = ret.get(0);
                
                for( k = 0; k < l.size() + 1; k++){
                    ArrayList<Integer> t = new ArrayList<Integer>(l);
                    t.add(k,num[i]);
                    ret.add(t);
                }
                ret.remove(l);
            }
        }
        return ret;
    }
    

    O(n!) ?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.