java solution using arraylist


  • 0
    H
    public class Solution {
        
    
        public List<List<Integer>> permuteUnique(int[] nums) {
        
           List<Integer> input = IntStream.of(nums).boxed().collect(Collectors.toList());
           
            return permuteUnique(input);
            
        }
        
        public List<List<Integer>> permuteUnique(List<Integer> input) {
    
            List<List<Integer>> result = new ArrayList<>();
    
            if(input.size() == 1){
                result.add(input);
                return result;
            }
            
            List<List<Integer>>  allRightPermutations = permuteUnique(input.subList(1,input.size()));
            int left = input.get(0);
            
            for(List<Integer> perm : allRightPermutations){
                
                for(int i = 0 ; i<= perm.size(); i++){
                    List<Integer> blah = new ArrayList<>(perm);
                    blah.add(i,left);
                    if(! result.contains(blah)){
                        result.add(blah);
                    }
                }
                
            }
            
            return result;
            
        }
        
        
        
    }
    

Log in to reply
 

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