Killing LeetCode [46] 全排列

Description

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

Intro

Ref Link:https://leetcode.cn/problems/permutations/
Difficulty:Medium
Tag:Array,Back Tracking
Updated Date:2023-09-20

Test Cases

示例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 中的所有整数 互不相同

思路 1

  • 暴力解法,递归

Code AC 1

class Solution {
    public List<List<Integer>> permute1(int[] nums) {
        return permuteRecursive(nums, nums.length);
    }

    public List<List<Integer>> permuteRecursive(int[] nums, int length) {
        if (length == 1) {
            List<List<Integer>> result = new ArrayList<>();
            LinkedList<Integer> start = new LinkedList<>();
            start.add(nums[0]);
            result.add(start);
            return result;
        } else {
            return merge(permuteRecursive(nums, length - 1), nums[length - 1]);
        }
    }

    public List<List<Integer>> merge(List<List<Integer>> list, Integer one) {
        List<List<Integer>> result = new ArrayList<>();
        for (List<Integer> item : list) {
            for (int i = 0; i <= item.size(); i++) {
                result.add(insert(item, one, i));
            }
        }
        return result;
    }

    LinkedList<Integer> insert(List<Integer> list, Integer one, int index) {
        LinkedList<Integer> newList = new LinkedList<>();
        newList.addAll(list);
        newList.add(index, one);
        return newList;
    }
}

Accepted 1

26/26 cases passed (1 ms)
Your runtime beats 77.93 % of java submissions
Your memory usage beats 29.85 % of java submissions (42.7 MB)

思路 2

  • 回溯算法

  • 排列问题需要一个used数组,标记已经选择的元素,一个排列里一个元素只能使用一次

  • 递归终止条件:当收集元素的数组path的大小达到和nums数组一样大的时候,说明找到了一个全排列,此时要收集结果。

  • 排列问题是回溯算法解决的经典题目,排列问题和组合问题的不同点: 每层都是从0开始搜索而不是startIndex ; 需要used数组记录path里都放了哪些元素了文章来源地址https://uudwc.com/A/Bvkn4

Code AC 2

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if (nums == null || nums.length == 0)
            return result;
        boolean[] used = new boolean[nums.length];
        subPermute(nums, used, result, new ArrayList<>());
        return result;
    }

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

Accepted 2

26/26 cases passed (1 ms)
Your runtime beats 77.93 % of java submissions
Your memory usage beats 92.98 % of java submissions (42.3 MB)

复杂度分析

  • 时间复杂度:O(n * n!)
  • 空间复杂度:O(n)

原文地址:https://blog.csdn.net/m0_37292477/article/details/133079992

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

上一篇 2023年09月24日 14:27
微信小程序更改AI类目-深度合成-AI绘画/AI问答教程
下一篇 2023年09月24日 14:28