Permutations: what's wrong in my code?


  • 0
    C
    public class Solution {
    public List<List<Integer>> permute(int[] nums) {
        return permute(nums, 0);
    }
    
    public List<List<Integer>> permute(int[] nums, int ind) { // ind: the element to be inserted to [ind+1:end]
        List<List<Integer>> all = new LinkedList<List<Integer>>();
        if( nums == null) return all; // error case
        else if( ind > nums.length - 1 ) return all;
        
        int first = nums[ind]; // get the ind-th int
        List<List<Integer>> subAll = permute(nums, 1 + ind );
    
        for(List<Integer> temp : subAll) {
            List<Integer> temp = subAll.get(j);
            int N = temp.size();
            for(int i = 0; i <= N; i++) 
            {
                List<Integer> newlist = new LinkedList<Integer>(temp);
                newlist.add(i, first);
                all.add(newlist);
            }
        }
        return all;
    }
    

    }

    Any hints welcomed.
    Thanks


Log in to reply
 

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