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 ...
随机推荐
- dp培训完结(8.9)
概率与期望dp 期望: 为什么下面的式子成立? 若x可以取1,2,3,则x+c可以取1+c,2+c,3+c..........x*c可以取1*c,2*c,3*c why? 举个例子(E(x+y)=E( ...
- Fragment 基础使用及重叠问题
一 基本使用 Fragment依附于Activity使用,方面我们在一个页面里面切换显示多屏内容. Activity管理Fragment有两种方式,通过FragmentTransacation这个类来 ...
- 邻近双线性插值图像缩放的Python实现
最近在查找有关图像缩放之类的算法,因工作中需要用到诸如此类的图像处理算法就在网上了解了一下相关算法,以及其原理,并用Python实现,且亲自验证过,在次与大家分享. 声明:本文代码示例针对的是plan ...
- lazyload懒加载插件
在main.js中引入vue-lazyload插件 并使用 注册插件: import VueLazyLoad from 'vue-lazyload' Vue.use(VueLazyLoad,{ lo ...
- tail()函数
与head()函数类似,默认是取dataframe中的最后五行. 函数源码: def tail(self, n=): """ Returns last n rows &q ...
- robot framework :List Variables-List变量及其用法
[转自:https://blog.csdn.net/yezibang/article/details/52692342] 这一讲我们重点来介绍List Variables-List变量及其用法. 一. ...
- UI自动化之js\jquery的应用
js\jquery的应用,有很多难以定位到的,可以通过js或者jquery来处理 目录 1.js 2.jquery 1.js 1.1js有5种定位,最后execute_script(js)来执行js ...
- ubuntu环境下重启mysql服务报错“No directory, logging in with HOME=-”
前提:使用系统的环境 3.13.0-24-generic mysql的版本:5.6.33 错误描述: 首先用mysqld_safe启动报错如下: root@zabbix-forFunction:~# ...
- linux上搭建nginx+ftp,实现文件的上传与访问
ftp服务器搭建 1.新建用户ftpuser并指定主目录为/home/ftpuser (注意:这个目录是后面存储和读取文件的目录) <!--创建用户并指定主目录--> useradd -d ...
- TNS-01106: Listener using listener name LISTENER has already been started
-- 启动监听,提示已经启动. [oracle@sh ~]$ lsnrctl startLSNRCTL for Linux: Version 12.1.0.2.0 - Production on 06 ...