2024-04-03 代码随想录

代码随想录算法训练营day29 | 题目491、题目46、题目47


题目一描述

491. 非递减子序列

给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。

数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。

示例 1:
输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

示例 2:
输入:nums = [4,4,3,2,1]
输出:[[4,4]]

提示:
1 <= nums.length <= 15
-100 <= nums[i] <= 100

解题思路

注意本题不能对原数组排序,同时要做树层的去重。
used初始化放在for循环前面,这样才是针对每一层做记录。

代码实现

方法一:

class Solution {

    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        backtracking(nums, res, path, 0);
        return res;
    }

    private void backtracking(int[] nums, List<List<Integer>> res, List<Integer> path, int startIndex) {
        if (path.size() >= 2) {
            res.add(new ArrayList<>(path));
        }
        // 不能对数组排序的时候,树层去重的方法:第一次进入这一层时构建辅助数组或者集合,哈希表,记录本层的使用过的元素。
        // 不是全局变量,而是局部变量,仅对本层有效。
        boolean[] used = new boolean[201]; // 放在循环体前面才是每层首次进入时被执行。
        for (int i = startIndex; i < nums.length; i++) {
            if (used[nums[i] + 100]) {
                continue;
            }
            if (path.size() > 0 && nums[i] < path.get(path.size() - 1)) {
                continue;
            }
            path.add(nums[i]);
            used[nums[i] + 100] = true;
            backtracking(nums, res, path, i + 1);
            path.remove(path.size() - 1);
        }
    }
}

题目二描述

给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

示例 1:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:

输入:nums = [0,1]
输出:[[0,1],[1,0]]
示例 3:

输入:nums = [1]
输出:[[1]]

提示:

1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums 中的所有整数 互不相同

解题思路

注意这里used数组其实不应该存值,存nums数组下标即可,这里只是因为nums 中的所有整数 互不相同所以才能这样做。

代码实现

方法一:

class Solution {
    boolean[] used = new boolean[21];

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        backtracking(res, path, nums);
        return res;
    }

    private void backtracking(List<List<Integer>> res, List<Integer> path, int[] nums) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[nums[i] + 10]) {
                continue;
            }
            path.add(nums[i]);
            used[nums[i] + 10] = true;
            backtracking(res, path, nums);
            path.remove(path.size() - 1);
            used[nums[i] + 10] = false;
        }
    }
}

方法二:

class Solution {
    boolean[] used = new boolean[10];

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        backtracking(res, path, nums);
        return res;
    }

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

题目三描述

47. 全排列 II

给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。

示例 1:
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]

示例 2:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

提示:

1 <= nums.length <= 8
-10 <= nums[i] <= 10

解题思路

可以用不排序的每层记录并去重。
也可以先排序,然后根据used数组对树层去重。

代码实现

方法一:

class Solution {
    boolean[] used = new boolean[10];

    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        backtracking(res, path, nums);
        return res;
    }

    private void backtracking(List<List<Integer>> res, List<Integer> path, int[] nums) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        boolean[] layerUsed = new boolean[21];
        for (int i = 0; i < nums.length; i++) {
            if (layerUsed[nums[i] + 10]) {
                continue;
            }
            if (used[i]) {
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            layerUsed[nums[i] + 10] = true;
            backtracking(res, path, nums);
            path.remove(path.size() - 1);
            used[i] = false;
        }
    }
}

方法二:

class Solution {
    boolean[] used = new boolean[10];

    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        Arrays.sort(nums);
        backtracking(res, path, nums);
        return res;
    }

    private void backtracking(List<List<Integer>> res, List<Integer> path, int[] nums) {
        if (path.size() == nums.length) {
            res.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]) { // 不加下标判断就是在对树枝去重
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backtracking(res, path, nums);
            path.remove(path.size() - 1);
            used[i] = false;
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容