【LeetCode】46. Full arrangement+47. Full arrangement II

Topic link: 46. ​​Full permutation
Topic: Given a sequence without repeated numbers, return all possible full permutations.
Example:

输入: [1,2,3]
输出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

Idea: dfs+backtracking, use a tag array to record whether the number at each position has been used. If it has not been used, add the number at the current position to the list. The recursive boundary is that the length of the list is equal to the length of nums, and record at this time The answer is entered into ans. After adding a round of answers, backtracking is performed, the mark is removed, and the last added element is removed at the same time.
Code:

class Solution {
    
    
    public void dfs(int []nums,boolean vis[],List<Integer> list,List<List<Integer>> ans){
    
    
        if(nums.length==list.size()){
    
    
            ans.add(new ArrayList<>(list));//注意这里如果直接添加list添加的是引用
            return;
        }
        for(int i=0;i<nums.length;i++){
    
    
            if(!vis[i]){
    
    
                list.add(nums[i]);
                vis[i]=true;
                dfs(nums,vis,list,ans);
                vis[i]=false; //回溯去标记
                list.remove(list.size()-1);
            }
        }
    }
    public List<List<Integer>> permute(int[] nums) {
    
    
        List<List<Integer>> ans=new ArrayList<>();
        boolean vis[]=new boolean[nums.length];
        for(int i=0;i<nums.length;i++) vis[i]=false;
        dfs(nums,vis,new ArrayList<>(),ans);
        return ans;
    }
}

This question is actually similar to 47. Full Permutation II , except that the latter does not allow repetitions, so when you record the answer, you can replace it with a set.

class Solution {
    
    

    public void dfs(int nums[],boolean vis[],List<Integer> list,Set<List<Integer>> ans){
    
    
        if(nums.length==list.size()){
    
    
            ans.add(new ArrayList<>(list));
            return ;
        }
        for(int i=0;i<nums.length;i++){
    
    
            if(!vis[i]){
    
    
                list.add(nums[i]);
                vis[i]=true;
                dfs(nums,vis,list,ans);
                vis[i]=false; //回溯删除标记
                list.remove(list.size()-1);
            }
        }
    }
    public List<List<Integer>> permuteUnique(int[] nums) {
    
    
        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        if(nums.length==1){
    
    
            List<Integer> list=new ArrayList<>();
            for(int num:nums){
    
    
                list.add(num);
            }
            ans.add(list);
            return ans;
        }
        List<Integer> list=new ArrayList<>();
        boolean vis[]=new boolean[nums.length];
        for(int i=0;i<nums.length;i++) vis[i]=false;
        Set<List<Integer>> listSet=new HashSet<>();
        dfs(nums,vis,list,listSet);
        ans.addAll(listSet);
        return ans;
    }
}

Similar topics include: 77. Combination +39. Combination Sum

Guess you like

Origin blog.csdn.net/dl962454/article/details/115022914