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

相关推荐
兴科Sinco24 分钟前
[leetcode 1]给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出和为目标值 target 的那两个整数[力扣]
python·算法·leetcode
沐怡旸25 分钟前
【算法--链表】138.随机链表的复制--通俗讲解
算法·面试
anlogic31 分钟前
Java基础 9.10
java·开发语言·算法
薛定谔的算法34 分钟前
JavaScript单链表实现详解:从基础到实践
数据结构·算法·leetcode
CoovallyAIHub43 分钟前
CostFilter-AD:用“匹配代价过滤”刷新工业质检异常检测新高度! (附论文和源码)
深度学习·算法·计算机视觉
幻奏岚音43 分钟前
《数据库系统概论》第一章 初识数据库
数据库·算法·oracle
你好,我叫C小白44 分钟前
贪心算法(最优装载问题)
算法·贪心算法·最优装载问题
CoovallyAIHub1 小时前
CVPR 2025 | 频率动态卷积(FDConv):以固定参数预算实现频率域自适应,显著提升视觉任务性能
深度学习·算法·计算机视觉
mit6.8241 小时前
[rStar] 解决方案节点 | `BaseNode` | `MCTSNode`
人工智能·python·算法
晴空闲雲2 小时前
数据结构与算法-树和二叉树-二叉树的存储结构(Binary Tree)
数据结构·算法