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

相关推荐
远远远远子几秒前
C++-- 内存管理
c++·算法
sprintzer17 分钟前
10.6-10.15力扣模拟刷题
算法·leetcode·职场和发展
徐子童18 分钟前
算法---队列+宽搜
算法··队列·层序遍历
一念&22 分钟前
每日一个C语言知识:C 数组
c语言·开发语言·算法
小年糕是糕手24 分钟前
【数据结构】单链表“0”基础知识讲解 + 实战演练
c语言·开发语言·数据结构·c++·学习·算法·链表
咸鱼爱学习1 小时前
【题解】B2613【深基1.习5】打字速度
数据结构·c++·算法
云青黛2 小时前
轮廓系数(一个异型簇的分类标准)
人工智能·算法·机器学习
云青黛2 小时前
肘部法找k
人工智能·算法·机器学习·聚类
CHANG_THE_WORLD2 小时前
if条件语句 三目运算符 汇编分析
汇编·算法·条件语句·if 语句·汇编分析·条件语句汇编分析
tumu_C2 小时前
无用知识研究:在trailing return type利用decltype,comma operator在对函数进行sfinae原创 [二]
开发语言·c++·算法