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

示例:

输入: [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. kafka 生产者发送消息

    KafkaProducer 创建一个 KafkaThread 来运行 Sender.run 方法. 1. 发送消息的入口在 KafkaProducer#doSend 中,但其实是把消息加入到 batc ...

  2. 监测工具dstat使用说明

    参考地址: https://blog.csdn.net/sinat_34789167/article/details/80986709

  3. ugui代码设置ui锚点

    using UnityEngine; public enum AnchorPresets { TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCente ...

  4. docker搭建一个渗透测试环境 bwapp为例

    bwapp是一个渗透测试靶场,他其中中含有100多个Web漏洞  基本涵盖了所有主要的已知Web漏洞,包括OWASP Top 10的各种 首先要去搜索一下  看一下有哪些镜像可以下载    docke ...

  5. github javascript相关项目star数排行榜(前30,截止2016.11.18):

    github javascript相关项目star数排行榜(前30,截止2016.11.18): 前端开源框架 TOP 100 前端 TOP 100:::::https://www.awesomes. ...

  6. Flask使用原生sql语句

    安装pip install flask-sqlalchemy 创建数据库对象 # 设置数据库连接地址app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://r ...

  7. 027 (H5*) react01

    目录 正文 1:创建Vue项目 1: 全局安装 vue-cli cnpm install --global vue-cli 2: 创建一个基于 webpack 模板的新项目 vue init webp ...

  8. 前端 CSS 一些标签默认有padding

    一个html body标签 默认有 margin外边距属性 比如ul标签,有默认的padding-left值. 那么我们一般在做站的时候,是要清除页面标签中默认的padding和margin.以便于我 ...

  9. 数据映射-LSM Tree和SSTable

    Coming from http://blog.sina.com.cn/s/blog_693f08470101njc7.html 今天来聊聊lsm tree,它的全称是log structured m ...

  10. Hadoop Map/Reduce

    Hadoop Map/Reduce是一个使用简易的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错的方式并行处理上T级别的数据集.一个Map/Reduce ...