46. Permutations --- leetcode 算法笔记
来源:互联网 发布:汽修厂管理接待软件 编辑:程序博客网 时间:2024/06/05 15:28
Given a collection of distinct numbers, return all possible permutations.
思路:若前n个数已经排好,则第n+1个数只需插入到前n个数中即形成前n+1个数的全部排列。即数学归纳法的思想。
代码:
public List<List<Integer>> permute(int[] nums) { List<List<Integer>> permutations = new ArrayList<List<Integer>>() ; for(int i = 0 ;i < nums.length ;i ++){ permutations = permute(permutations,nums[i]) ; } return permutations ; } private List<List<Integer>> permute(List<List<Integer>> permutations, int num){ if(permutations.size() <= 0){ List<Integer> permutation = new ArrayList<Integer>() ; permutation.add(num) ; permutations.add(permutation) ; return permutations ; }else{ List<List<Integer>> ret = new ArrayList<List<Integer>>() ; for(List<Integer> permutation : permutations){ for(int i = 0 ;i <= permutation.size() ; i++){ List<Integer> p = new ArrayList<Integer>(permutation) ; p.add(i,new Integer(num)) ; ret.add(p) ; } } return ret ; } }
0 0
- 46. Permutations --- leetcode 算法笔记
- LeetCode笔记:46. Permutations
- 算法Week08.04 - LeetCode 46. Permutations
- LeetCode算法题目:Permutations
- [leetCode刷题笔记]46. Permutations
- LeetCode算法题之Permutations
- [LeetCode]46.Permutations
- LeetCode 46.Permutations
- 46. Permutations Leetcode Python
- LeetCode --- 46. Permutations
- [Leetcode] 46. Permutations
- [leetcode] 46.Permutations
- LeetCode 46. Permutations DFS
- 46. Permutations LeetCode
- Leetcode 46. Permutations
- leetcode 46. Permutations
- LeetCode *** 46. Permutations
- LeetCode 46. Permutations
- N2N 安装方法,适用于 多台电脑由于处于各自内网环境,不能互通, 但是能够访问一台公共的外网服务器, 通过该服务器实现多台内网服务器直接的互连互通
- JAVA值传递与引用传递
- 聊聊Linux 五种IO模型
- HTTP深入浅出 http请求
- 认识循环
- 46. Permutations --- leetcode 算法笔记
- 对Activity管理(附源码理解)
- linux驱动模块开机自动加载,以及应用程序开机自启动
- Robot framwork case执行命令
- jQuery – 自动完成(Autocomplete)
- 设置mysql外部ip可以访问的命令
- matlab-自控原理 已知传递函数,求其阶跃响应
- spring MVC配置详解
- Maven pom.xml 配置详解