算法day22-回溯(4)

目录

  1. 递增子序列
  2. 全排列
  3. 全排列II
  4. 重新安排行程
  5. N皇后
  6. 解数独

一、递增子序列

 https://leetcode.cn/problems/non-decreasing-subsequences/description/?envType=problem-list-v2&envId=8At1GmaZ

 

class Solution {
    Set<List<Integer>> res = new HashSet<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        dfs(0, nums);
        return new ArrayList<>(res);
    }
    public void dfs(int start, int[] nums){
        if(path.size() >= 2){
            res.add(new ArrayList<>(path));
        }
        if(start == nums.length){
            return;
        }
        for(int end = start; end < nums.length; end++){
            
            if((path.size() > 0 && nums[end] >= path.get(path.size()-1)) || path.size() == 0){
                path.add(nums[end]);
                dfs(end+1, nums);
                path.remove(path.size()-1);
            }
        }
    }
}

 

二、全排列

https://leetcode.cn/problems/permutations/?envType=problem-list-v2&envId=8At1GmaZ

 

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] visited;
    public List<List<Integer>> permute(int[] nums) {
        Arrays.sort(nums);
        visited = new int[nums.length];
        dfs(0, nums);
        return res;
    }
    public void dfs(int start, int[] nums){
        if(start == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int j = 0; j < nums.length; j++){
            if(j > 0 && nums[j] == nums[j-1])   continue;   //不能与上一个相等
            if(visited[j] == 0){
                path.add(nums[j]);
                visited[j] = 1;
                dfs(start + 1, nums);
                visited[j] = 0;
                path.remove(path.size()-1);
            }
        }
    }
}

 

三、全排列II

https://leetcode.cn/problems/permutations-ii/?envType=problem-list-v2&envId=8At1GmaZ

 

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] visited;
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        visited = new int[nums.length];
        dfs(0, nums);
        return res;
    }
    public void dfs(int start, int[] nums){
        if(start == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int j = 0; j < nums.length; j++){
            //被选上的重复数在前一个位置和当前位置的选择不冲突
            //若当前数字前面有重复的,但前面那个数字没被选择,那就不能选择当前数字(回溯过后来的)
            if(j > 0 && nums[j] == nums[j-1] && visited[j - 1] == 0)   continue;   //不能与上一个相等
            if(visited[j] == 0){
                path.add(nums[j]);
                visited[j] = 1;
                dfs(start + 1, nums);
                visited[j] = 0;
                path.remove(path.size()-1);
            }
        }
    }
}

 

四、重新安排行程

https://leetcode.cn/problems/reconstruct-itinerary/?envType=problem-list-v2&envId=8At1GmaZ

 

 

五、N皇后

https://leetcode.cn/problems/n-queens/?envType=problem-list-v2&envId=8At1GmaZ

 

六、解数独

https://leetcode.cn/problems/sudoku-solver/?envType=problem-list-v2&envId=8At1GmaZ

 

posted @ 2025-05-22 14:36  筱倩  阅读(8)  评论(0)    收藏  举报