(回溯05) 非递减子序列 全排列 全排列II

一、非递减子序列

力扣第491题

该题新加入 set 集合用来去重,判断当前层是否有重复元素

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> sequence = new ArrayList<>();
    int count = 0;
    public List<List<Integer>> findSubsequences(int[] nums) {
        
        backtracking(nums, 0);
        return result;
    }

    public void backtracking(int[] nums, int index) {
        Set<Integer> set = new HashSet<>();
        if(index == nums.length) return;

        for(int i = index; i < nums.length; i++) {
            
            if(!set.add(nums[i])) {
                continue;
            }
            if(count != 0 && nums[i] < sequence.get(sequence.size() - 1)) {
                continue;
            }
            sequence.add(nums[i]);
            count++;
            if(count >= 2) {
                result.add( new ArrayList(sequence));
            }
            backtracking(nums, i + 1);
            sequence.remove(sequence.size() - 1);
            count--;
        }
    }
}

时间复杂度:O(n*)

空间复杂度:O(n)

二、全排列

力扣第46题

利用数组去掉已经选出来的元素

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] used = new int[21];
    public List<List<Integer>> permute(int[] nums) {
        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[nums[i] + 10] == 1) {
                continue;
            }
            used[nums[i] + 10] = 1;
            path.add(nums[i]);
            backtracking(nums);
            path.remove(path.size() - 1);
            used[nums[i] + 10] = 0;
        }
    }
}

时间复杂度:O(n!)

空间复杂度:O(n)

三、全排列 II

力扣第47题

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] used = new int[21];
    public List<List<Integer>> permute(int[] nums) {
        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[nums[i] + 10] == 1) {
                continue;
            }
            used[nums[i] + 10] = 1;
            path.add(nums[i]);
            backtracking(nums);
            path.remove(path.size() - 1);
            used[nums[i] + 10] = 0;
        }
    }
}

时间复杂度:O(n! * n)

空间复杂度:O(n)

相关推荐
蒙奇D索大11 分钟前
【数据结构】图论最短路径算法深度解析:从BFS基础到全算法综述
数据结构·算法·图论·广度优先·图搜索算法
trouvaille12 分钟前
哈希数据结构的增强
算法·go
我不是小upper27 分钟前
L1和L2核心区别 !!--part 2
人工智能·深度学习·算法·机器学习
liujing102329292 小时前
Day09_刷题niuke20250609
java·c++·算法
不7夜宵2 小时前
力扣热题100 k个一组反转链表题解
算法·leetcode·链表
蒟蒻小袁3 小时前
力扣面试150题--课程表
算法·leetcode·面试
闻缺陷则喜何志丹3 小时前
【动态规划】B4336 [中山市赛 2023] 永别|普及+
c++·算法·动态规划·洛谷
不二狗4 小时前
每日算法 -【Swift 算法】电话号码字母组合
开发语言·算法·swift
AL流云。4 小时前
【优选算法】分治
数据结构·算法·leetcode·排序算法
C++ 老炮儿的技术栈9 小时前
UDP 与 TCP 的区别是什么?
开发语言·c++·windows·算法·visual studio