Day 29 回溯05

491.递增子序列 

题目链接:491. 非递减子序列 - 力扣(LeetCode)

思路:

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        backTracking(nums, 0);
        return result;
    }

    public void backTracking(int[] nums, int start){
        if(path.size() >= 2)
            result.add(new ArrayList<>(path));            
        HashSet<Integer> hs = new HashSet<>();
        for(int i = start; i < nums.length; i++){
            if(!path.isEmpty() && path.get(path.size() -1 ) > nums[i] || hs.contains(nums[i]))
                continue;
            hs.add(nums[i]);
            path.add(nums[i]);
            backTracking(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }
}

46.全排列 

题目链接:46. 全排列 - 力扣(LeetCode)

​​​​​​​思路:起始位置不变,利用辅助数组,选了的元素为true

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    Boolean[] used;

    public List<List<Integer>> permute(int[] nums) {
        if(nums.length == 0){
            return result;
        }
        used = new Boolean[nums.length];
        for(int i = 0; i < nums.length; i++) {
            used[i] = false;
        }
        backtracking(nums);
        return result;
    }

    public void backtracking(int[] nums){
        if(path.size() == nums.length){
            result.add(new ArrayList<>(path));
            return;
        }

        for(int i = 0; i < nums.length; i++){
            if(used[i]) continue;
            used[i] = true;
            path.add(nums[i]);
            backtracking(nums);
            path.removeLast();
            used[i] = false;
        }
    }
}

47.全排列 II 

题目链接:47. 全排列 II - 力扣(LeetCode)

思路:

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        boolean[] used = new boolean[nums.length];
        Arrays.fill(used, false);
        Arrays.sort(nums);
        backTrack(nums, used);
        return result;
    }

    private void backTrack(int[] nums, boolean[] used) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
                continue;
            }
             if (used[i] == false) {
                used[i] = true;
                path.add(nums[i]);
                backTrack(nums, used);
                path.remove(path.size() - 1);
                used[i] = false;
             }
        }
    }
}

相关推荐

  1. Day 29 回溯05

    2024-03-23 12:14:04       42 阅读
  2. day27 回溯03

    2024-03-23 12:14:04       69 阅读
  3. Day 24 回溯算法01

    2024-03-23 12:14:04       36 阅读
  4. Day25- 回溯算法part05

    2024-03-23 12:14:04       57 阅读
  5. day27回溯

    2024-03-23 12:14:04       56 阅读
  6. day 24 第七章 回溯算法part01

    2024-03-23 12:14:04       27 阅读
  7. day 27 第七章 回溯算法part03

    2024-03-23 12:14:04       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 12:14:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 12:14:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 12:14:04       82 阅读
  4. Python语言-面向对象

    2024-03-23 12:14:04       91 阅读

热门阅读

  1. 探索MySQL中的SQL_MODE数据模式

    2024-03-23 12:14:04       39 阅读
  2. vue中如何用一个数组减去另一个数组

    2024-03-23 12:14:04       38 阅读
  3. node和npm yarn包管理工具

    2024-03-23 12:14:04       34 阅读
  4. npm 常用命令详解

    2024-03-23 12:14:04       43 阅读
  5. SAP-FI配置与业务解析之代发(客户)销售作业

    2024-03-23 12:14:04       40 阅读
  6. 将uint8_t数组转成uint32_t

    2024-03-23 12:14:04       46 阅读
  7. C++继承

    2024-03-23 12:14:04       40 阅读
  8. html5&css&js代码 038 列表

    2024-03-23 12:14:04       44 阅读
  9. Ubuntu下轻松搭建Wordpress:舞动Docker的魔法

    2024-03-23 12:14:04       37 阅读