【力扣算法】46-全排列

题目

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/permutations
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

方法1:回溯法

回溯法 是一种通过探索所有可能的候选解来找出所有的解的算法。如果候选解被确认 不是 一个解的话(或者至少不是 最后一个 解),回溯算法会通过在上一步进行一些变化抛弃该解,即 回溯 并且再次尝试。

这里有一个回溯函数,使用第一个整数的索引作为参数 backtrack(first)。

  • 如果第一个整数有索引 n,意味着当前排列已完成。

  • 遍历索引 first 到索引 n - 1 的所有整数。Iterate over the integers from index first to index n - 1.

    • 在排列中放置第 i 个整数, 即 swap(nums[first], nums[i]).
    • 继续生成从第 i 个整数开始的所有排列: backtrack(first + 1).
    • 现在回溯,即通过 swap(nums[first], nums[i]) 还原.
class Solution {
  public void backtrack(int n,
                        ArrayList<Integer> nums,
                        List<List<Integer>> output,
                        int first) {
    // if all integers are used up
    if (first == n)
      output.add(new ArrayList<Integer>(nums));
    for (int i = first; i < n; i++) {
      // place i-th integer first 
      // in the current permutation
      Collections.swap(nums, first, i);
      // use next integers to complete the permutations
      backtrack(n, nums, output, first + 1);
      // backtrack
      Collections.swap(nums, first, i);
    }
  }

  public List<List<Integer>> permute(int[] nums) {
    // init output list
    List<List<Integer>> output = new LinkedList();

    // convert nums into list since the output is a list of lists
    ArrayList<Integer> nums_lst = new ArrayList<Integer>();
    for (int num : nums)
      nums_lst.add(num);
    
    int n = nums.length;
    backtrack(n, nums_lst, output, 0);
    return output;
  }
}

复杂性分析

时间复杂度: O ( ∑ k = 1 N P ( N , k ) ) \mathcal{O}(\sum_{k = 1}^{N}{P(N, k)}) O(k=1NP(N,k)) P ( N , k ) = N ! ( N − k ) ! = N ( N − 1 ) . . . ( N − k + 1 ) P(N, k) = \frac{N!}{(N - k)!} = N (N - 1) ... (N - k + 1) P(N,k)=(Nk)!N!=N(N1)...(Nk+1),该式被称作 n 的 k-排列,或者_部分排列_.
为了简单起见,使 first + 1 = k. 这个公式很容易理解:对于每个 k (每个first) 有 N(N - 1) … (N - k + 1) 次操作, 且 k 的范围从 1 到 N (first 从 0 到 N - 1).

我们来做一个结果的粗略估计: N ! ≤ ∑ k = 1 N N ! ( N − k ) ! = ∑ k = 1 N P ( N , k ) ≤ N × N ! N! \le \sum_{k = 1}^{N}{\frac{N!}{(N - k)!}} = \sum_{k = 1}^{N}{P(N, k)} \le N \times N! N!k=1N(Nk)!N!=k=1NP(N,k)N×N!, 即算法比 O ( N × N ! ) \mathcal{O}(N \times N!) O(N×N!) 更优 且 比 O ( N ! ) \mathcal{O}(N!) O(N!) 稍慢.

空间复杂度: O ( N ! ) \mathcal{O}(N!) O(N!) 由于必须要保存 N! 个解。

作者:LeetCode
链接:https://leetcode-cn.com/problems/two-sum/solution/quan-pai-lie-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

感想

随便写了一个,还以为会超时,没想到直接过了。第一次提交错了是因为注释的地方,忘记了链表传递的是引用。

执行用时 :4 ms, 在所有Java提交中击败了70.55%的用户

内存消耗 :36.9 MB, 在所有Java提交中击败了93.84%的用户

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) return null;
        List<List<Integer>> res = new LinkedList<>();
        if (nums.length == 1) {
            List<Integer> resList = new LinkedList<>();
            resList.add(nums[0]);
            res.add(resList);
            return res;
        } else if (nums.length == 2) {
            List<Integer> resList = new LinkedList<>();
            resList.add(nums[0]);
            resList.add(nums[1]);
            res.add(resList);
            List<Integer> resList2 = new LinkedList<>();//这里如果不new一个新的,而在原来resList上操作的话,会导致添加进res里的结果也被改变。原因是链表传递的是引用
            resList2.add(nums[1]);
            resList2.add(nums[0]);
            res.add(resList2);
            return res;
        } else {
            List<List<Integer>> formerRes = permute(Arrays.copyOfRange(nums, 1, nums.length));
            for (List<Integer> formerList : formerRes) {
                for (int i = 0; i <= nums.length - 1; i++) {
                    List<Integer> newList = new LinkedList<>(formerList);
                    newList.add(i, nums[0]);
                    res.add(newList);
                }
            }
            return res;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值