[LeetCode-Java]46. Permutations
来源:互联网 发布:nw.js electron 编辑:程序博客网 时间:2024/06/06 12:35
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]
]
解1:使用递归,每次添加不重复的数,添加满则加入到结果集中。9ms。
public class Solution { List<List<Integer>> result = null; public List<List<Integer>> permute(int[] nums) { if (nums == null || nums.length == 0) return result; result = new ArrayList<>(); dfs(nums,0,new ArrayList<Integer>()); return result; } void dfs(int[] nums, int start, List<Integer> list){ if (start == nums.length) { result.add(new ArrayList<Integer>(list)); return; } for (int i = 0;i<nums.length;i++){ if (!list.contains(nums[i])) { list.add(nums[i]); dfs(nums, start + 1, list); list.remove(list.size()-1); } } }}
0 0
- (Java)LeetCode-46. Permutations
- [LeetCode-Java]46. Permutations
- 46.Permutations leetcode java
- [leetcode]46. Permutations@Java解题报告
- [Leetcode] Permutations (Java)w
- [Leetcode] Permutations II (Java)
- [LeetCode][Java] Permutations
- [LeetCode][Java] Permutations II
- [leetcode-46]Permutations(java)
- [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
- Android Studio 集成信鸽推送
- javascript中的作用域
- 4-初始化和构造器
- 【33.33%】【codeforces 681D】Gifts by the List
- 1619-7 张良 十月十九号总结 [连续第十九天]
- [LeetCode-Java]46. Permutations
- Intellij IDEA 利用gradle从maven仓库中加载第三方包
- Dialog的几种类型
- JavaScript 浏览器事件解析
- CodeForces 560A Currency System in Geraldion 【水题】
- uboot命令体系和命令的执行
- R_1.介绍与入门
- Win10下Cisco VPN Client安装教程
- Django分页代码HTML