2024-04-03 代碼隨想錄

代碼隨想錄算法訓(xùn)練營day29 | 題目491、題目46、題目47


題目一描述

491. 非遞減子序列

給你一個整數(shù)數(shù)組 nums ,找出并返回所有該數(shù)組中不同的遞增子序列,遞增子序列中 至少有兩個元素 。你可以按 任意順序 返回答案。

數(shù)組中可能含有重復(fù)元素,如出現(xiàn)兩個整數(shù)相等,也可以視作遞增序列的一種特殊情況。

示例 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

解題思路

注意本題不能對原數(shù)組排序,同時要做樹層的去重。
used初始化放在for循環(huán)前面,這樣才是針對每一層做記錄。

代碼實現(xiàn)

方法一:

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));
        }
        // 不能對數(shù)組排序的時候,樹層去重的方法:第一次進(jìn)入這一層時構(gòu)建輔助數(shù)組或者集合,哈希表,記錄本層的使用過的元素。
        // 不是全局變量,而是局部變量,僅對本層有效。
        boolean[] used = new boolean[201]; // 放在循環(huán)體前面才是每層首次進(jìn)入時被執(zhí)行。
        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);
        }
    }
}

題目二描述

給定一個不含重復(fù)數(shù)字的數(shù)組 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 中的所有整數(shù) 互不相同

解題思路

注意這里used數(shù)組其實不應(yīng)該存值,存nums數(shù)組下標(biāo)即可,這里只是因為nums 中的所有整數(shù) 互不相同所以才能這樣做。

代碼實現(xiàn)

方法一:

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

給定一個可包含重復(fù)數(shù)字的序列 nums ,按任意順序 返回所有不重復(fù)的全排列。

示例 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

解題思路

可以用不排序的每層記錄并去重。
也可以先排序,然后根據(jù)used數(shù)組對樹層去重。

代碼實現(xiàn)

方法一:

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) { // 加下標(biāo)判斷就是在對樹層去重
                continue;
            }
            if (used[i]) { // 不加下標(biāo)判斷就是在對樹枝去重
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backtracking(res, path, nums);
            path.remove(path.size() - 1);
            used[i] = false;
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 代碼隨想錄算法訓(xùn)練營day28 | 題目93、題目78、題目90 題目一描述 93. 復(fù)原 IP 地址[https...
    超甜de瓜閱讀 201評論 0 0
  • 代碼隨想錄算法訓(xùn)練營day2 | 題目977、題目 209、題目 59 題目一描述 977. 有序數(shù)組的平方[ht...
    超甜de瓜閱讀 74評論 0 0
  • 代碼隨想錄算法訓(xùn)練營day7 | 題目454、題目383、題目15、題目18 題目一描述 454. 四數(shù)相加 II...
    超甜de瓜閱讀 140評論 0 0
  • 代碼隨想錄算法訓(xùn)練營day13 | 題目239、題目347、題目155 題目一描述 239. 滑動窗口最大值[ht...
    超甜de瓜閱讀 100評論 0 0
  • 代碼隨想錄算法訓(xùn)練營day20 | 題目654、題目998、題目617、題目700、題目98 題目一描述 654....
    超甜de瓜閱讀 242評論 0 0

友情鏈接更多精彩內(nèi)容