LeetCode--046--全排列(java)
给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [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)的更多相关文章
- [leetcode] 46. 全排列(Java)
46. 全排列 这题我们可以借用31. 下一个排列写的nextPermutation函数来做,稍微改造一下即可 注意要先给nums排个序 class Solution { // 当没有下一个排列时re ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:全排列【46】
LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...
- Java实现 LeetCode 47 全排列 II(二)
47. 全排列 II 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] class Solut ...
- Java实现 LeetCode 46 全排列
46. 全排列 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2] ...
- Java for LeetCode 046 Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...
- LeetCode 046 Permutations 全排列
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have th ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
随机推荐
- 安装python是提示 0x80072f7d 错误的解决办法
最简单的方法: Internet 选项-> 高级里面 勾选使用TLS1.1和使用TLS1.2即可.实际测试是ok的
- Powershell&TFS_Part 1
目录 目录 前言 TFS 对象模型 Powershell Powershell面向对象 Powershell默认会在PC中设置执行脚本权限 调试脚本 断点 Step Microsoft Visual ...
- import * as 用法
- 测开之路一百一十三:bootstrap媒体对象
实现效果,左边是图片或者其他媒体,右边是对应的描述 引入bootstrap和jquery标签 class="media" 数量多一些看着就会很规整 <!DOCTYPE htm ...
- oracle--对象权限
sys用户查询ww用户创建的表(已经commited) select * from ww.wwTable; 普通用户lisi查询ww用户的表 grant select on wwTable to li ...
- Java核心技术
[Java核心技术36讲]1.谈谈你对Java平台的理解 2.Exception和Error有什么区别 3.谈谈final.finally.finalize有什么不同?4.强引用.软引用.弱引用.虚引 ...
- mysql树查询、递归查询
关键词:mysql树查询,mysql递归查询 转自:http://www.cnblogs.com/c-h-y/p/9420726.html 之前一直用的是Oracle,对于树形查询可以使用start ...
- java枚举详解
枚举的本质是类,枚举是用来构建常量数据结构的模板(初学者可以以此方式理解: public static final X=xxx),枚举的使用增强了程序的健壮性,在引用一个不存在的枚举值的时候,编译器会 ...
- TimeUnit类 java.util.concurrent.TimeUnit
TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段 主要作用 时间颗粒度转换 延时 常用的颗粒度 TimeUnit.DAYS ...
- java synchronized实现可见性对比volatile
问题: 大家可以先看看这个问题,看看这个是否有问题呢? 那里有问题呢? public class ThreadSafeCache { int result; public int getResult( ...