【回溯】Leetcode 46. 全排列【中等】

全排列

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

示例:

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

解题思路

  • 1、使用回溯算法来生成所有可能的全排列。
  • 2、从第一个位置开始,依次将每个数字放入当前位置,并递归生成下一个位置的排列。
  • 3、当所有位置都填满时,将当前排列加入结果集。

具体步骤

  • 1、初始化一个空列表 result,用于存储所有可能的全排列。
  • 2、编写一个递归函数 backtrack,该函数接受当前正在处理的子数组 nums、 以及当前正在构建的排列 tempList。
  • 3、在 backtrack 中,如果 current 的长度等于 nums 的长度,
  • 说明已经构建出了一个完整的排列,将其加入到 result 中。
  • 4、否则,遍历数组 nums,对于每个未被使用过的数字,将其添加到 tempList 中,并递归调用 backtrack 处理剩余的子数组。
  • 5、在递归调用完成后,要将刚刚添加到 tempList 中的数字从中删除,以便后续使用。

Java实现

java 复制代码
public class Permutation {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        backtrack(nums, new ArrayList<>(), result);
        return result;
    }

    private void backtrack(int[] nums, List<Integer> tempList, List<List<Integer>> result) {
        if (tempList.size() == nums.length) {
            result.add(new ArrayList<>(tempList));
        } else {
            for (int i = 0; i < nums.length; i++) {
                if (tempList.contains(nums[i])) continue; // element already exists, skip
                tempList.add(nums[i]);
                backtrack(nums, tempList, result);
                //回溯一层后,对应移除上一层的末尾元素
                tempList.remove(tempList.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
        Permutation permutation = new Permutation();

        // Test Case 1
        int[] nums1 = {1, 2, 3};
        List<List<Integer>> result1 = permutation.permute(nums1);
        System.out.println("Test Case 1:");
        printResult(result1);

        // Test Case 2
        int[] nums2 = {0, 1};
        List<List<Integer>> result2 = permutation.permute(nums2);
        System.out.println("Test Case 2:");
        printResult(result2);
    }

    private static void printResult(List<List<Integer>> result) {
        for (List<Integer> list : result) {
            System.out.println(list);
        }
        System.out.println();
    }
}

时间空间复杂度

  • 时间复杂度:O(N!),其中N是数组nums的长度。因为生成全排列的数量是N的阶乘。

  • 空间复杂度:O(N!),存储所有可能的全排列。

相关推荐
2401_87622134几秒前
因数个数、因数和、因数积
c++·算法
云里雾里!11 分钟前
LeetCode 744. 寻找比目标字母大的最小字母 | 从低效到最优的二分解法优化
算法·leetcode
一条大祥脚25 分钟前
26.1.3 快速幂+容斥 树上dp+快速幂 带前缀和的快速幂 正序转倒序 子序列自动机 线段树维护滑窗
数据结构·算法
二狗哈30 分钟前
czsc入门5: Tick RawBar(原始k线) NewBar (新K线)
算法·czsc
Tisfy35 分钟前
LeetCode 0865.具有所有最深节点的最小子树:深度优先搜索(一次DFS + Python5行)
算法·leetcode·深度优先·dfs·题解
Q741_14739 分钟前
C++ 队列 宽度优先搜索 BFS 力扣 429. N 叉树的层序遍历 C++ 每日一题
c++·算法·leetcode·bfs·宽度优先
Yzzz-F41 分钟前
P4145 上帝造题的七分钟 2 / 花神游历各国[线段树 区间开方(剪枝) + 区间求和]
算法·机器学习·剪枝
Zzz不能停43 分钟前
堆排序算法及大小堆区别
数据结构·算法
zd8451015001 小时前
stm32f407 电机多轴联动算法
stm32·单片机·算法
代码游侠1 小时前
应用——Linux FrameBuffer图形显示与多线程消息系统项目
linux·运维·服务器·开发语言·前端·算法