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

示例:

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

无奈,用swap的方法从左向右滑动,直到最后结果和最初的一致停止,只适用于三位数。。。。(改进一下让每个数字作为第一位后面的进行滑动,应该可以pass,放弃)

错:

 class Solution {
public static void swap(int[] nums_,int a,int b){
int temp = nums_[a];
nums_[a] = nums_[b];
nums_[b] = temp;
}
public static boolean isEqual(int[] a,int[] b){
for(int i = 0;i < a.length;i++){
if(a[i] != b[i])return false;
}
return true;
}
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList();
List<Integer> lists = new ArrayList();
if(nums.length < 2){
lists.add(nums[0]);
res.add(lists);
return res;
}
int[] nums_ = new int[nums.length];
for(int k = 0;k < nums.length;k++){
nums_[k] = nums[k];
lists.add(nums[k]); }
res.add(new ArrayList(lists));
lists.removeAll(lists);
swap(nums_,0,1);
for(int j = 0;j < nums.length;j++){
lists.add(nums_[j]);
}
res.add(new ArrayList(lists));
int i = 1;
while(!isEqual(nums,nums_)){
if(i+1<nums.length){
swap(nums_,i,i+1);
if(!isEqual(nums,nums_)){
lists.removeAll(lists);
for(int j = 0;j < nums.length;j++){
lists.add(nums_[j]);
}
res.add(new ArrayList(lists));
}
i++;
}else{
i = 0;
}
}
return res;
} }

正确做法bt:  添加顺序就是[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]

[[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2],

[2, 1, 3, 4], [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1],

[3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1],

[4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]

TIME:O(N!*N)(整体来说)

SPACE:O(N)

 class Solution {

     public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums== null || nums.length ==0)return res;
helper(res,new ArrayList<>(),nums);
return res;
}
public void helper(List<List<Integer>> res,List<Integer> list,int[] nums){
if(list.size() == nums.length){
res.add(new ArrayList<>(list));
return;
}
for(int i = 0;i < nums.length;i++){
if(list.contains(nums[i]))continue;//contaisn的时间复杂度为O(N)
list.add(nums[i]);
helper(res,list,nums);
list.remove(list.size()-1);
}
} }

如果递归符合T(n) = T(n-1)+T(n-2)+....T(1)+T(0)  时间复杂度基本符合O(2^n),如果在其中的一些步骤可以省略,则可以简化为O(n!)

对于方法1思想的完善:

TIME:O(N)

SPACE:O(N)

 class Solution {

     public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums.length == 0 || nums == null)return res;
helper(res,0,nums);
return res;
}
public void helper(List<List<Integer>> res,int start,int[] nums){
if(start == nums.length){
List<Integer> list = new ArrayList<>();
for(int q:nums){
list.add(q);
}
res.add(new ArrayList<>(list));
return;
}
for(int i = start;i < nums.length;i++){
swap(nums,start,i);
helper(res,start+1,nums);
swap(nums,start,i);
} }
public void swap(int[] nums,int l,int m){
int temp = nums[l];
nums[l] = nums[m];
nums[m] = temp;
} }

2019-05-04 10:45:10

LeetCode--046--全排列(java)的更多相关文章

  1. [leetcode] 46. 全排列(Java)

    46. 全排列 这题我们可以借用31. 下一个排列写的nextPermutation函数来做,稍微改造一下即可 注意要先给nums排个序 class Solution { // 当没有下一个排列时re ...

  2. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  3. LeetCode:全排列【46】

    LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...

  4. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  5. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

  6. Java实现 LeetCode 47 全排列 II(二)

    47. 全排列 II 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] class Solut ...

  7. Java实现 LeetCode 46 全排列

    46. 全排列 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2] ...

  8. Java for LeetCode 046 Permutations

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

  9. LeetCode 046 Permutations 全排列

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

  10. [LeetCode] Permutations 全排列

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

随机推荐

  1. LinkedBlockingDeque 源码分析

    LinkedBlockingDeque LinkedBlockingDeque 能解决什么问题?什么时候使用 LinkedBlockingDeque? 1)LinkedBlockingDeque 是基 ...

  2. hbase报错之 Master is initializing

    报错日志 ERROR: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing at org.apache.hadoop ...

  3. bzoj-2525 Dynamite

    Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了烟火,现在需要点燃M个点上的引线引爆所有的烟火.某个点上的引线被点燃后的1单位时间内,在树上和它相邻的点的引线会被点燃.如 ...

  4. 应用安全-Web安全-子域名/相关域名

    技巧 DNS解析记录 主站获取 单点登录接口 crossdomain.xml IP反查 通过HTTPS证书收集 DNS域传送搜集 联系人信息/邮箱反查域名 x-dns-prefetch-control ...

  5. 建立 Active Directory域 ----学习笔记

    第五章 建立 Active Directory域 1.工作组和域的理解 ​ a.工作组是一种平等身份环境,各个计算机之间各个为一个独立体,不方便管理和资源共享. ​ b.域环境一般情况下满足两类需求, ...

  6. 20191105 《Spring5高级编程》笔记-第12章

    第12章 使用Spring远程处理 12.4 在Spring中使用JMS 使用面向消息的中间件(通常成为MQ服务器)是另一种支持应用程序间通信的流行方法.消息队列(MQ)服务器的主要优点在于为应用程序 ...

  7. 接口框架 python+unittest+request+HTMLTestRunner

    request的各种方法主要用来处理客户端浏览器提交的请求中的各项参数和选项.如POST,GET最常用的两种请求 官方文档:http://docs.python-requests.org/en/mas ...

  8. “希希敬敬对”团队--‘百度贴吧小爬虫’Alpha版本展示博客

    希希敬敬对的 Alpha阶段测试报告 随笔链接地址 https://www.cnblogs.com/xiaoyoushang/p/10078826.html   Alpha版本发布说明 随笔链接地址 ...

  9. js Object的属性 Configurable,Enumerable,Writable,Value,Getter,Setter

    对象的数据属性 Configurable,Enumerable,Writable,Value var person = {} Object.defineProperty(person,'name',{ ...

  10. MySQL里null与空值的辨析

    CREATE TABLE `test` ( `col1` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `co ...