Can anyone help me figure out why it TLO


  • 0
    W
    public class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        result.add(new ArrayList<Integer>());
        Arrays.sort(nums);
        for(int i = 0;i<nums.length;i++)
            result = getPermu(result,nums[i]);
        return result;
    }
    public List<List<Integer>> getPermu(List<List<Integer>> list,int c){
        int n = list.size(),m = list.get(0).size();
        List<List<Integer>> result = new ArrayList<>();
        
        for(int i = 0;i<n;i++){
            Integer last = null;
            for(int j = 0;j<=m;j++){
                List<Integer> temp = new ArrayList<Integer>(list.get(i));
                int last_temp = 0;
                if(j!=m)
                    last_temp = temp.get(j);
                if(last==null||last!=c){
                    temp.add(j,c);
                    result.add(temp);
                }
                last = last_temp;
            }
        }
        return result;
    }
    

    }


  • 0
    S

    usually, the TLO is caused by duplicated list in your result. Try comparing the size of your result with the exact number of permutations.


Log in to reply
 

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