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

示例:

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

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

错:

 1 class Solution {
 2     public static void swap(int[] nums_,int a,int b){
 3             int temp = nums_[a];
 4             nums_[a] = nums_[b];
 5             nums_[b] = temp;
 6         }
 7     public static boolean isEqual(int[] a,int[] b){
 8         for(int i = 0;i < a.length;i++){
 9             if(a[i] != b[i])return false;
10         }
11         return true;
12     }
13     public List<List<Integer>> permute(int[] nums) {
14         List<List<Integer>> res = new ArrayList();
15             List<Integer> lists = new ArrayList();
16             if(nums.length < 2){
17                 lists.add(nums[0]);
18                 res.add(lists);
19                 return res;
20             }
21             int[] nums_ = new int[nums.length];
22             for(int k = 0;k < nums.length;k++){
23                 nums_[k] = nums[k];
24                 lists.add(nums[k]);
25
26             }
27             res.add(new ArrayList(lists));
28             lists.removeAll(lists);
29             swap(nums_,0,1);
30             for(int j = 0;j < nums.length;j++){
31                 lists.add(nums_[j]);
32             }
33             res.add(new ArrayList(lists));
34             int i = 1;
35             while(!isEqual(nums,nums_)){
36                 if(i+1<nums.length){
37                     swap(nums_,i,i+1);
38                     if(!isEqual(nums,nums_)){
39                         lists.removeAll(lists);
40                         for(int j = 0;j < nums.length;j++){
41                             lists.add(nums_[j]);
42                         }
43                         res.add(new ArrayList(lists));
44                     }
45                     i++;
46                 }else{
47                     i = 0;
48                 }
49             }
50             return res;
51     }
52
53 }

正确做法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)

 1 class Solution {
 2
 3     public List<List<Integer>> permute(int[] nums) {
 4         List<List<Integer>> res = new ArrayList<>();
 5         if(nums== null || nums.length ==0)return res;
 6         helper(res,new ArrayList<>(),nums);
 7         return res;
 8     }
 9     public void helper(List<List<Integer>> res,List<Integer> list,int[] nums){
10         if(list.size() == nums.length){
11             res.add(new ArrayList<>(list));
12             return;
13         }
14         for(int i = 0;i < nums.length;i++){
15             if(list.contains(nums[i]))continue;//contaisn的时间复杂度为O(N)
16             list.add(nums[i]);
17             helper(res,list,nums);
18             list.remove(list.size()-1);
19         }
20     }
21
22 }

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

对于方法1思想的完善:

TIME:O(N)

SPACE:O(N)

 1 class Solution {
 2
 3     public List<List<Integer>> permute(int[] nums) {
 4         List<List<Integer>> res = new ArrayList<>();
 5         if(nums.length == 0 || nums == null)return res;
 6         helper(res,0,nums);
 7         return res;
 8     }
 9     public void helper(List<List<Integer>> res,int start,int[] nums){
10         if(start == nums.length){
11             List<Integer> list = new ArrayList<>();
12             for(int q:nums){
13                 list.add(q);
14             }
15             res.add(new ArrayList<>(list));
16             return;
17         }
18         for(int i = start;i < nums.length;i++){
19             swap(nums,start,i);
20             helper(res,start+1,nums);
21             swap(nums,start,i);
22         }
23
24     }
25     public void swap(int[] nums,int l,int m){
26         int temp = nums[l];
27         nums[l] = nums[m];
28         nums[m] = temp;
29     }
30
31 }

2019-05-04 10:45:10

转载于:https://www.cnblogs.com/NPC-assange/p/10807727.html

LeetCode--046--全排列(java)相关推荐

  1. 《LeetCode力扣练习》第46题 全排列 Java

    <LeetCode力扣练习>第46题 全排列 Java 一.资源 题目: 给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 .你可以 按任意顺序 返回答案. 示例 1: 输 ...

  2. LeetCode 47 全排列 II -- 回溯法

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/permutations-ii 题意: 给定一个可包含重复数字的序列 nums ,按任意顺序 返 ...

  3. LeetCode 47. 全排列 II

    文章目录 解法1:回溯 + 剪枝 牛客网的全排列 https://leetcode-cn.com/problems/permutations-ii/ 难度:中等   给定一个可包含重复数字的序列,返回 ...

  4. Leetcode怎么调试java代码,IDEA2020.1使用LeetCode插件运行并调试本地样例的方法详解...

    环境: idea2020.1 插件: LeetCode-editor 6.7 一.IDEA安装LeetCode插件 安装完成重启idea 打开插件 URL可以选择国服和世界服.LoginName和Pa ...

  5. leetcode 47. 全排列 II 思考分析

    题目 给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列. 思考分析以及代码 这一题和前面的做过的两个题目有所关联: leetcode 46. 全排列 思考分析 再加上lee ...

  6. leetcode 组合总和(Java)

    leetcode题目 组合总和 -- leetcode 39 题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target , 找出 candidates 中所有可以使数字和为 ...

  7. leetcode 相交链表 java

    相交链表 题干 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5] ...

  8. 拉马努金的整数拆分全排列JAVA实现非递归

    拉马努金的整数拆分全排列JAVA实现非递归 点这里: 递归方式 结果是:组合成100的可能性共有:190569292种!! 不做详细说明了,需要看文字描述的,点上面链接跳转递归方式,查看详细说明 来, ...

  9. 全排列 leetcode java_LeetCode--046--全排列(java)

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

  10. leetcode算法题解(Java版)-11-贪心大法

    一.全排列变式(递归) 题目描述 Given a collection of numbers that might contain duplicates, return all possible un ...

最新文章

  1. IDEA:com.intellij.execution.ExecutionException: not found for the web module.
  2. Docker原理剖析
  3. 关于es6的一些文章
  4. 简历不会做?集设网社区带来精致的个人网站供你参考
  5. 怎么把照片做成计算机主题,windows10主题制作怎么操作_windows10电脑主题如何自己制作...
  6. Linux窗口字是倒着的,linux反撇号怎么打出来
  7. hive hql 交差并集 练习
  8. 芯科Zigbee应用程序框架
  9. Spring XML 注入
  10. 如何在Windows下使用DOS命令进入MySQL数据库?
  11. Mit6.S081-xv6参考书翻译
  12. 根据火车的出发时间和到达时间,编写程序计算整个旅途所用的时间。比如G198次列车从青岛站出发时间为16:00,到达北京南站的时间为20:40,则整个旅途所用时间为04:40。
  13. 苹果8怎么投屏到电视_手机怎么投屏到电视?苹果手机投屏的三种方法
  14. python爬虫解析库(Xpath、beautiful soup、Jsonpath)
  15. 网关状态检测 echo request/reply
  16. qiankun微前端主子应用通信方案
  17. 三相同步电动机的平衡方程式
  18. 【操作系统】-- 先来先服务算法(FCFS)、短作业优先算法(SJF)、高响应比调度算法(HRRN)
  19. 【数据压缩(四)】c语言实现BMP序列转YUV文件并播放
  20. 【数电】74161的同步与异步级联

热门文章

  1. 基于现有工程解读stm32的工程文件
  2. java 庖丁解牛api_Java Restful API Best Practices
  3. python 解压缩 tar 包 或 tar.gz包
  4. 12.前K个高频元素---使用优先队列和哈希表解决
  5. 数学知识--Unconstrained Optimization(第一章)
  6. 打造自己的树莓派监控系统1--CPU监控-matplotlib显示数据
  7. Pytorch的优化器推荐
  8. go string 转 uint64_小改动,大提升:最近 Go 标准库的一次优化
  9. 2021年普高考成绩查询,山东2021年高考成绩改为6月26日前公布
  10. mysql中的EXPLAIN