(回溯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)

相关推荐
冻柠檬飞冰走茶2 分钟前
PTA基础编程题目集 7-15 计算圆周率(C语言实现)
c语言·开发语言·数据结构·算法
问商十三载10 分钟前
AI 引擎生成式优化两种思路怎么选?2026 对比分析附选型方法
人工智能·算法
hanlin031 小时前
刷题笔记:力扣第704、977、209题(数组相关)
笔记·算法·leetcode
陕西企来客2 小时前
生成式引擎优化(GEO)行业白皮书:从流量分配到认知占有的战略跃迁
人工智能·算法·机器学习·技术好geo优化
清泓y2 小时前
深度学习算法
算法·ai·语言模型·面试
m沐沐2 小时前
【深度学习】循环神经网络RNN——结构、原理与长期依赖问题解析
人工智能·pytorch·python·rnn·深度学习·算法·机器学习
Rabitebla2 小时前
C++ 内存管理全面复习:从内存分布到 operator new/delete
java·c语言·开发语言·c++·算法·leetcode
玖玥拾3 小时前
LeetCode 58 最后一个单词的长度
算法·leetcode
LONGZETECH3 小时前
工业实训仿真设计实践:电机拆装软件的 DAG 流程建模、工具精度分级与数据体系搭建
大数据·算法·unity·架构·汽车