Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]
]

给定一个数组,求所有的排列组合。

做出这道题很简单,主要是要比较快的速度,第一次做,耗时5ms,比较慢,主要就是用递归,每次向arraylist中放入一个数(不重复),放够数目的数就result.add();

public class Solution {public List<List<Integer>> permute(int[] nums) {List<List<Integer>> result = new ArrayList<List<Integer>>();if( nums.length == 0)return result;getResult(result,nums,new ArrayList<Integer>(),0);return result;}public static void getResult(List<List<Integer>> result,int[] nums,ArrayList<Integer> ans,int num){if( num == nums.length){result.add(new ArrayList<Integer>(ans));return ;}for( int i = 0 ;i <nums.length;i++){if( !ans.contains(nums[i]) ){ans.add(nums[i]);getResult(result,nums,ans,num+1);ans.remove((Integer)nums[i]);}}   }
}

还有一种方法,用迭代,一个数字的时候,就是数字本身,两个数字的时候,就是将第二数字放在第一个数字的左边和右边两种,然后依次类推,在第n个数字的时候,就是在n-1的基础上,每一个空位(每个数字的左边和右边,共n个)放入第n个数字。耗时4ms

public class Solution {public static List<List<Integer>> permute(int[] nums) {List<List<Integer>> result = new ArrayList<List<Integer>>();if (nums.length == 0)return result;List<Integer> l0 = new ArrayList<Integer>();l0.add(nums[0]);result.add(l0);for( int i = 1;i<nums.length;i++){List<List<Integer>> l1 = new ArrayList<List<Integer>>();for( List<Integer> ll : result ){for(int j =0; j<=i;j++ ){List<Integer> l2 = new ArrayList<Integer>(ll);l2.add(j, nums[i]);l1.add(l2); }}result = l1;         }return result;}
}

讲两种方法综合一下,递归+迭代,这样是3ms

public class Solution {public List<List<Integer>> permute(int[] nums) {List<List<Integer>> result = new ArrayList<List<Integer>>();List<Integer> element = new ArrayList<Integer>();getResult(result, element, nums, 0);return result;}private void getResult(List<List<Integer>> result, List<Integer> element, int[] nums, int n){if(n==nums.length){result.add(element);return;}int size = element.size();for(int i = 0; i <= size; i++){List<Integer> temp = new ArrayList<Integer>(element); temp.add(i,nums[n]);getResult(result, temp, nums, n+1);}return;}
}

然后还有一种方法,有n位数,那么第1位有n种情况,在确定第一位的情况下,第2位就有n-1种情况,以此类推,使用数组就可以使运行时间更快,用时2ms

public class Solution {public List<List<Integer>> permute(int[] nums) {List<List<Integer>> result = new ArrayList<List<Integer>>();if (nums.length == 0)return null;int len = nums.length - 1;result = getResult(nums, 0, len, result);return result;}public List<List<Integer>> getResult(int[] nums, int k, int len,List<List<Integer>> result) {if (k == len) {List<Integer> subList = new ArrayList<Integer>();for (int i = 0; i < nums.length; i++) {subList.add(nums[i]);}result.add(subList);} else {for (int i = k; i <= len; i++) {int temp = nums[k];nums[k] = nums[i];nums[i] = temp;getResult(nums, k + 1, len, result);int temp1 = nums[k];nums[k] = nums[i];nums[i] = temp1;}}return result;}
}

转载于:https://www.cnblogs.com/xiaoba1203/p/5686683.html

leetcode 46 Permutations ----- java相关推荐

  1. 【数字全排列】LeetCode 46. Permutations

    LeetCode 46. Permutations Solution0: 补充一个偷鸡摸狗的方法.偷懒的做法直接使用std::next_permutation()函数 class Solution { ...

  2. leetCode 46. Permutations 回溯问题 | Medium

    46. Permutations(全排列问题--回溯问题经典) Given a collection of distinct numbers, return all possible permutat ...

  3. LeetCode 46. Permutations

    46. Permutations Given a collection of distinct numbers, return all possible permutations. For examp ...

  4. 【数字全排列】LeetCode 47. Permutations II

    LeetCode 47. Permutations II Solution1:我的答案 笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~ class Solution { p ...

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

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

  6. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations.For exa ...

  7. leetcode 组合总和(Java)

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

  8. leetcode 相交链表 java

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

  9. leetcode 46 java,leetcode46.java

    package leetcode; import java.util.ArrayList; import java.util.List; /** * Created by 林剑 on 2016/10/ ...

  10. Leetcode每日一题:46.permutations(全排列)

    思路: 也可以用递归来求解 但是对应开销要大 且递归的核心部分也没有变化 //非递归法 #include <iostream> #include <vector> using ...

最新文章

  1. Git学习笔记07-删除文件
  2. easyui datagrid 每行数据添加 按钮
  3. hdu 4148 Length of S(n) (坑爹的规律题)
  4. 局域网屏幕共享_ShareMouse for Mac(鼠标键盘共享)
  5. 特斯拉提升安全监控等级,推出“哨兵模式”
  6. c语言 异或_C语言经典例题来袭!5大方法告诉你答案
  7. html2canvas生成海报的各种问题
  8. VB中FSO的调用的两种方法
  9. linux系统镜像安装方法,linux系统安装的引导镜像制作流程分享
  10. 第一次参加公司研发部门高级别会议之感
  11. Java 简单论文查重程序(SimHash+海明距离算法)
  12. oracle方案对象有表空间吗,oracle数据库中,实例、表空间、用户、方案、表、数据的关系例子...
  13. 如何用木板做桥_木头做桥基,300年不倒(组图)
  14. 追光的人对Echo,SkyReach的Beta产品测试报告
  15. 中国多媒体与网络教学学报杂志中国多媒体与网络教学学报杂志社中国多媒体与网络教学学报编辑部2022年第6期目录
  16. 第一行代码-第二版(郭霖著)笔记七(Content Provider)
  17. 如何设计签名 我的名字
  18. Python-分割PDF文件-如何自定义分割-按页数分割PDF-PyPDF2
  19. F5 ELK可视化方案如何做到DNS运维更安全更高效
  20. Gitee 图床被屏蔽后,我搭建了一个文件系统并封装成轮子开源

热门文章

  1. WCF服务编程-非WCF应用程序使用WCF服务
  2. antd-pro1.0使用jest对react组件进行单元测试
  3. FTP协议的命令与返回码
  4. Practical Node.js摘录(2018版)第1,2章。
  5. 搜索控制器UISearchController的使用
  6. Window 远程连接 Ubuntu 系统
  7. HDU2179--pi(麦金公式)
  8. ubuntu下eclipse搭建安卓开发环境
  9. 场景能量初显,这里有小程序的11个新发现(附2018年7月微信小程序TOP100榜单暨研究报告)...
  10. Head First 设计模式目录