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

示例:

输入: [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. centos 7 安装Telnet并设为开机自启动、开防火墙端口

    [root@b ~]# rpm -qa | grep telnettelnet-0.17-64.el7.x86_64telnet-server-0.17-64.el7.x86_64[root@b ~] ...

  2. (转)grep命令

    1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局 ...

  3. pthon之mock应用

    研发过程中常见分工合作开发接口,但互相之间接口有依赖,这时候便可以使用mock 目录 1.安装 2.使用mock调试自己写的方法 3.使用mock解除依赖关系 1.安装 由于我的是python2.7, ...

  4. python2.7+appium环境搭建

    实现android自动化 目录 1.环境准备.安装包准备 2.安装 3.验证环境搭建成功 1.环境准备.安装包准备 第一步:环境准备: java环境 python环境 第二步:准备安装包 Node.j ...

  5. nw打包vue项目exe更换图标

    web项目用nw打包好了之后发现没办法更换桌面显示图标问题,找了一下发现大多推荐Resource进行最后更换,试了第一次怎么也不管用,电脑重启了一下就行了...... 首先下载安装好了Resource ...

  6. MM理论

    最初的MM理论,即由美国的Modigliani和Miller(简称MM)教授于1958年6月份发表于<美国经济评论>的“资本结构.公司财务与资本”一文中所阐述的基本思想.该理论认为,在不考 ...

  7. lateral view 使用方法

    这个函数相当于拆开行变成列. 可以理解为行转列. select id,order_label from table_bx lateral view explode(split(work_order_l ...

  8. mysql多对多查询 原生写法

    准备工作,1.创建表 CREATE TABLE IF NOT EXISTS `users` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `name` VARCHAR ...

  9. Java容器框架总结(一)

    本篇根据<Java编程思想> 第11章 持有对象 整理,总结Java容器框架中常用集合类及接口的特点及使用场景. (一)总结 1)数组将数字与对象联系起来:可以保存基本类型的数据:一旦生成 ...

  10. 14 (H5*) JS第4天 函数、作用域、预解析

    目录 1:函数的其他定义 2:函数作为参数 3:函数作为返回值 4:作用域 5:作用域链 6:预解析 7:预解析分段 复习 <script> /* * 复习: * 函数:把一些重复的代码封 ...