How to change code from PermutationsI [JAVA]


  • 0
    public class Solution {
        List<List<Integer>> result=new ArrayList<>();
        public List<List<Integer>> permuteUnique(int[] nums) {
            backtrack(nums,new ArrayList<Integer>(),0);
            return result;
        }
        
        private void backtrack(int nums[],ArrayList<Integer> list,int start){
            if(list.size()==nums.length){
                result.add(new ArrayList<Integer>(list));
                return;
            }
            
            for(int i=0;i<=list.size();i++){
                list.add(i,nums[start]);
                backtrack(nums,list,start+1);
                list.remove(i);
            }
        }     
    }
    

    this is my implementation in PermutationI, how to implement PermutationII via adding judgement in the loop rather than use HashSet or boolean arrays to avoid repeat list ?


  • 0
    S

    You have to add one boolean flag to check if the element is visited or not.


Log in to reply
 

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