资讯专栏INFORMATION COLUMN

[Leetcode] Permutations 全排列

scq000 / 1758人阅读

摘要:每一轮搜索选择一个数加入列表中,同时我们还要维护一个全局的布尔数组,来标记哪些元素已经被加入列表了,这样在下一轮搜索中要跳过这些元素。

Permutations I

</>复制代码

  1. Given a collection of numbers, return all possible permutations.

  2. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].

交换法 复杂度

时间 O(N^2) 空间 O(N) 递归栈

思路

置换实际上是给出所有的排列方式,同样是用深度优先搜索,不过为了避免重复选择的情况,我们要保证两点:第一,所有数必须是数组中的,第二,数组中每个数只能用不多于也不少于一次。如果我们要多带带写一个函数,来判断下一轮搜索该选择哪一个数就很麻烦了。这里有一个技巧,我们可以只将数两两交换,不过交换时只能跟自己后面的交换。

代码

</>复制代码

  1. public class Solution {
  2. List> res;
  3. public List> permute(int[] nums) {
  4. res = new LinkedList>();
  5. helper(nums, 0);
  6. return res;
  7. }
  8. public void helper(int[] nums, int i){
  9. // 找到转置完成后的解,将其存入列表中
  10. if(i == nums.length - 1){
  11. List list = new LinkedList();
  12. for(int j = 0; j < nums.length; j++){
  13. list.add(nums[j]);
  14. }
  15. res.add(list);
  16. }
  17. // 将当前位置的数跟后面的数交换,并搜索解
  18. for(int j = i; j < nums.length; j++){
  19. swap(nums, i , j);
  20. helper(nums, i + 1);
  21. swap(nums, i, j);
  22. }
  23. }
  24. private void swap(int[] nums, int i, int j){
  25. int tmp = nums[i];
  26. nums[i] = nums[j];
  27. nums[j] = tmp;
  28. }
  29. }
深度优先搜索 复杂度

时间 O(N) 空间 O(N) 递归栈

思路

我们还可以简单的使用深度优先搜索来解决这题。每一轮搜索选择一个数加入列表中,同时我们还要维护一个全局的布尔数组,来标记哪些元素已经被加入列表了,这样在下一轮搜索中要跳过这些元素。

代码

</>复制代码

  1. public class Solution {
  2. List> res;
  3. boolean[] used;
  4. public List> permute(int[] nums) {
  5. res = new LinkedList>();
  6. used = new boolean[nums.length];
  7. List tmp = new LinkedList();
  8. helper(nums, tmp);
  9. return res;
  10. }
  11. private void helper(int[] nums, List tmp){
  12. if(tmp.size() == nums.length){
  13. List list = new LinkedList(tmp);
  14. res.add(list);
  15. } else {
  16. for(int idx = 0; idx < nums.length; idx++){
  17. // 遇到已经加过的元素就跳过
  18. if(used[idx]){
  19. continue;
  20. }
  21. // 加入该元素后继续搜索
  22. used[idx] = true;
  23. tmp.add(nums[idx]);
  24. helper(nums, tmp);
  25. tmp.remove(tmp.size()-1);
  26. used[idx] = false;
  27. }
  28. }
  29. }
  30. }
Permutations II

</>复制代码

  1. Given a collection of numbers that might contain duplicates, return all possible unique permutations.

  2. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].

深度优先搜索 复杂度

时间 O(N) 空间 O(N) 递归栈

思路

这题和上题的深度优先搜索很相似,区别在于:1、要先将数组排序,确保重复的元素是在一起的。2、除了不能加入之前出现过的元素之外,还不能加入本轮搜索中出现过的元素。如何判断哪些元素本轮出现过呢?我们加过一个数字并搜索后,在这一轮中把剩余的重复数字都跳过就行了,保证每一轮只有一个unique的数字出现。这和Combination Sum II中跳过重复元素的方法是一样的,注意要判断nums[i] == nums[i + 1],因为for循环结束时i还会额外加1,我们要把i留在最后一个重复元素处。

代码

</>复制代码

  1. public class Solution {
  2. List> res;
  3. boolean[] used;
  4. public List> permuteUnique(int[] nums) {
  5. res = new LinkedList>();
  6. used = new boolean[nums.length];
  7. Arrays.sort(nums);
  8. List tmp = new LinkedList();
  9. helper(nums, tmp);
  10. return res;
  11. }
  12. private void helper(int[] nums, List tmp){
  13. if(tmp.size() == nums.length){
  14. List list = new LinkedList(tmp);
  15. res.add(list);
  16. } else {
  17. for(int idx = 0; idx < nums.length; idx++){
  18. // 遇到已经加过的元素就跳过
  19. if(used[idx]){
  20. continue;
  21. }
  22. // 加入该元素后继续搜索
  23. used[idx] = true;
  24. tmp.add(nums[idx]);
  25. helper(nums, tmp);
  26. tmp.remove(tmp.size()-1);
  27. used[idx] = false;
  28. // 跳过本轮的重复元素,确保每一轮只会加unique的数字
  29. while(idx < nums.length - 1 && nums[idx] == nums[idx + 1]){
  30. idx++;
  31. }
  32. }
  33. }
  34. }
  35. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64532.html

相关文章

  • leetcode 47 Permutations II

    摘要:题目详情题目要求输入一个可能会有重复数字的数组,要求我们输出可能组成的全排列无重复排列。可以用来实现,但这种实现方式复杂度高。另外一种实现思路是,新声明一个数组来存储中元素的使用状况。以这个数组为例。 题目详情 Given a collection of numbers that might contain duplicates, return all possible unique ...

    Cobub 评论0 收藏0
  • [Leetcode] Permutation Sequence 排列序列

    摘要:找规律复杂度时间空间思路由于我们只要得到第个全排列,而不是所有全排列,我们不一定要将所有可能都搜索一遍。根据全排列顺序的性质,我们可以总结出一个规律假设全排列有个数组成,则第个全排列的第一位是。然后将得到,这个就是下一轮的。 Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutati...

    testHs 评论0 收藏0
  • leetcode 46 Permutations

    摘要:例如有如下的全排列想法这道题是用回溯法的思想解决的。回溯法在包含问题的所有解的解空间树中,按照深度优先的策略,从根节点出发深度优先搜索,搜索到某个点的时候,先判断该节点是否包含问题的解,如果包含就继续探索,否则就逐层向根节点回溯。 题目详情 Given a collection of distinct numbers, return all possible permutations....

    jubincn 评论0 收藏0
  • leetcode47 Permutations II

    摘要:当前的值如果已经被使用过,则继续判断下一个数值。则当第一个被添加进结果集时,可以继续使用第二个作为元素添加进结果集从而生成。假设将表示为那么结果集中会确保永远在数值的前面,从而避免了和的重复情况出现。 题目要求 Given a collection of numbers that might contain duplicates, return all possible unique ...

    taoszu 评论0 收藏0
  • leetcode46 Permutation 排列组合

    摘要:题目要求也就是得出所有可能的排列组合结果解题思路和代码这题显然采用递归的思路。在这里,我采用实现队列,从队列头获得上一组的结果,和当前元素结合之后,将结果插入到队尾。 题目要求 Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the ...

    wendux 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<