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里都放了哪些元素了

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值