题目:求所有全排列

难度:Medium

题目内容

Given a collection of distinct integers, return all possible permutations.

翻译:给定一组各不相同的整数,返回所有可能的排列。

Example:

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

我的思路:每种情况中,每一个元素只出现一次,只是之间的顺序不同,那么马上就联想到和之前的那个利用队列求按键输出所有可能的字母组合题一样:LeetCode第[17]题(Java):Letter Combinations of a Phone Number,只要把其中思路稍作修改即可。

步骤:

1、首先创建一个队列ans,往里面放一个空的list;

2、对ans队列进行while循环,只要队头List的长度小于输入数组的长度,那么说明还没有排完则进入循环;

3、取出队头List,并且对输入数组进行循环,如果循环内发现当前List里没有某个元素,则复制队头List,然后把此元素传入,再把复制品放入ans队尾。

为什么要复制一个新的List?

  因为,List是一个引用类型,取出队头后的循环判断的都是同一个队头List,而不是新的。

以【1,2,3】为输入数组,图示步骤如下:

最初ansList:

【【】】

第一大轮while循环后(此一大轮包括若干轮):

【【1】【2】【3】】

第二大轮循环后:

【【1,2】,【1,3】,

【2,1】,【2,3】,

【3,1】,【3,2】】

第三大轮循环后:

【【1,2,3】,【1,3,2】,

【2,1,3】,【2,3,1】,

【3,1,2】,【3,2,1】】

完毕

我的代码:

     public List<List<Integer>> permute(int[] nums) {
LinkedList<List<Integer>> ans = new LinkedList<List<Integer>>();
ans.add(new ArrayList<Integer>());
while (ans.peek().size() < nums.length) {
List<Integer> temp = ans.poll();
for (int i = 0; i < nums.length; i++) {
if (!temp.contains(nums[i])) {
List<Integer> inner = new ArrayList<Integer>(temp); // 复制一个新的
inner.add(nums[i]);
ans.add(inner);
}
}
}
return ans;
}

我的复杂度:O(N3)  ———因为contains()方法其实也循环了一次。

编码过程中的问题

1、忘了要复制出一个新的;

2、忘了怎么复制新List: new ArrayList<Integer>(temp);

答案代码

 public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
backtrack(list, new ArrayList<>(), nums);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
if(tempList.size() == nums.length){
list.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(list, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

答案复杂度:O(N3

答案思路

利用了递归的思想,新建一个递归方法,传入 三个参数: 答案List 、元素List、输入数组

递归方法的内部逻辑:

1、如果当前元素List的长度已经与输入数组一样,那么说明此元素List已经排列完毕,所以直接加入答案List

2、否则,对输入数组进行循环,如果当前元素List没有包含某元素,则将此元素传入当前元素List,并调用递归方法传入( 答案List 、元素List、输入数组),再将当前元素List刚加入的那个元素给删了。

主要逻辑和我的方法是一个思想,就是对对输入数组进行循环,不包括则加入,并再回到最初判断。

不过有个有意思的地方就是,他没有用复制一个新的List来解决复用的问题,而是把新加入的那个元素(最后一个)给remove掉,和复制一个新的List是同样的效果,不过这种方法降低了空间复杂度

但是请【注意】,这个删除的方法并不适用我的那个方法

  因为,假如我在tempList加入新元素后直接add进入 ansList,然后再删去temp刚加入的新元素,代码如下:

                if (!temp.contains(nums[i])) {
temp.add(nums[i]);
ans.add(temp);
temp.remove(temp.size()-1);
}

那么由于temp是个List,所以加入进ans的也是一个引用,也就是说ans里的和我要做remove操作的List是同一个List,所以这段代码执行完后ans只会加入一个长度没有变化的tempList,所以ans的队头永远都是空的List,while循环判断就会进入死循环。

那为什么递归方法却可以用删除

  因为递归方法在remove()方法的前面,当执行到remove()的时候,关于新的tempList的操作已经完成了,所以此时remove然后变回原来的tempList不会产生任何影响。

【并且,“复制一个新的List”,在递归方法也是可以用的,请自行验证哈~】

扩展:47. Permutations II

给定一组可能包含重复(46题元素都不同)的数字,返回所有可能的惟一排列。(其实本题还和LeetCode第[78]题Subsets(求子集)扩展——第[90]题:Subsets 2 这个很像)

Example:

Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

 答案代码

 public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, boolean [] used){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
} else{
for(int i = 0; i < nums.length; i++){
if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue;
used[i] = true;
tempList.add(nums[i]);
backtrack(list, tempList, nums, used);
used[i] = false;
tempList.remove(tempList.size() - 1);
}
}
}

答案思路

相对于本题多出了以下几点不同

1、因为包含相同的元素,所以需要排序以方便判断左右相同元素;

2、因为每个tempList都是一个新的排列,然而在此排列中又必须要有标志位来标志本次排列中已经使用过的元素;

3、如果(当前元素是被使用过的    或者   (当前元素和前一个元素相等  并且  前一个元素是没被使用过的))

    此时说明此元素值是被使用过的,所以continue。

在此特地解释一下为什么要是“ 前一个元素是没被使用过的” ,没有打错就是没有使用过的。

  因为,每一次排列都是按顺序进行循环的,直到关于此元素的后续全部循环结束后才会回溯到此将此元素的used再设回为没使用,所以,如果循环到后一个元素,而前面的元素已经是没使用过的,则说明前面的组合已经全部放进答案List中去了。

举例:【-1,-1,-1,2】

以当首先对第一个元素开头的排列进行循环,假设已经以第一个元素和第二个元素开头的都已经全部排列,已经开始回溯到第一个元素和第三个元素开头了,

此时的used为【true,false,false,false】

而因为此时第三个元素和前一个元素(第二个元素)是相等的,而前一个已经为false,说明前一个已经是进行循环完毕而回溯过的,所以此时,第一个元素和第三个元素开头则为第一个元素和第二个元素开头的重复排列,所以跳过。

LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2的更多相关文章

  1. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  4. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  5. LeetCode 【46. Permutations】

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

  6. 【一天一道LeetCode】#46. Permutations

    一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...

  7. LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

    题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word ...

  8. LeetCode第[21][23]题(Java):Merge Sorted Lists

    题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...

  9. LeetCode(46)Permutations

    题目 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the fo ...

随机推荐

  1. mysql 中调用存储过程之后,连接断开不可用

    解决方法: 由 mysql_real_connect(&m_mysql,host,user,passwd,Db,0,NULL,0) == NULL 改为 mysql_real_connect( ...

  2. 使用ODBC 数据库 ,运行程序时 出现 “遇到不适当的参数”

    我知道的一种情况是 数据库打开了,没有关闭,再次调用数据库打开函数,会出现这样错误.当然是打开同一个数据库同一张表.

  3. 高德js API moveAlong 函数的一个错误解决

    使用覆盖物之一:点标记,让点标记沿着固定的路线移动. API 提供了现成的函数 moveAlong() 开始以为 实现移动很简单:分两部 1.准备好经纬度数组 2.调用moveAlong()函数.按照 ...

  4. 转!!java序列化

    1.序列化是干什么的?       简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object st ...

  5. delphi 模拟POST提交数据

    unit GetHttpInfo; interface uses Classes, WinINet, Sysutils, windows, IDURI, IdSSLOpenSSL , IdBaseCo ...

  6. django博客项目5:博客首页视图(2)

    真正的 Django 博客首页视图 在此之前我们已经编写了 Blog 的首页视图,并且配置了 URL 和模板,让 Django 能够正确地处理 HTTP 请求并返回合适的 HTTP 响应.不过我们仅仅 ...

  7. 【我的Android进阶之旅】 解决bug: Expected file scheme in URI: content://downloads/my_downloads/12

    一.错误描述 今天测试MM用HTC手机测试某个模块的时候crash了,抓log后发现是使用DownloadManager下载apk安装包然后自动安装的时候,抛了异常:java.lang.Illegal ...

  8. tornado requesthandler可以重写的方法

    一 :RequestHandler 一般我们继承tornado.web.RequestHandler 1,RequestHandler.initialize()一般用于初始化,第三个字典参数传入 cl ...

  9. Linux(7)- Nginx.conf主配置文件、Nginx虚拟主机/访问日志/限制访问IP/错误页面优化、Nginx反向代理、Nginx负载均衡

    一.Nginx.conf主配置文件 Nginx主配置文件conf/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的.一般,每个区块以一对大括号{}来表示开始与结束. 核心模 ...

  10. Android图片加载框架Picasso最全使用教程5

    在之前的四篇博客中,我们学习了所有的关于Picasso的主要方法,我们也对这个Picasso有了一个很深的认识,下面就主要对Picasso自身进行分析,这样的话,会让我们更了解Picasso的核心方法 ...