Permutations:排列
来源:互联网 发布:迅雷7优化版 编辑:程序博客网 时间:2024/06/05 19:10
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]]思路:这种题一定是递归,因为要给出所有方式,有一点特殊的是使用一个arraylist来暂存结果。!!!做了有一阵题了,这种简单题还是没做出来,惆怅。
class Solution { public void dfs(List<List<Integer>> list,List<Integer> l,int[] nums){ if(l.size()==nums.length){ list.add(new ArrayList<Integer>(l)); return; }else{ for(int i=0;i<nums.length;i++){ if(l.contains(nums[i])) continue; l.add(nums[i]); dfs(list,l,nums); l.remove(l.size()-1); } } } public List<List<Integer>> permute(int[] nums) { List<List<Integer>> list = new ArrayList<List<Integer>>(); dfs(list,new ArrayList<Integer>(),nums); return list; }}
阅读全文
0 0
- Permutations 排列
- Permutations:排列
- 全排列生成 Permutations
- Permutations II 排列II
- 全排列(permutations)
- leetcode:Permutations排列
- Permutations 排列的实现
- 全排列Permutations
- 生成排列 Generating Permutations
- leetcode:46. Permutations 排列
- LeetCode--Permutations 全排列
- LintCode permutations(全排列)
- LeetCode:Permutations, Permutations II(求全排列)
- leetcode--Permutations(打印所有排列)
- [LeetCode]—Permutations 求全排列
- LeetCode | Permutations(全排列)
- Permutations 排列的生成, 递归
- Permutations 数字的全排列
- PHP数据库字典[markdown版]
- Cday_11
- 动态规划——最大字段和
- pageoffer页面调用
- 分享一个MongoDB可视化工具rockmongo-on-windows
- Permutations:排列
- redis nosql使用
- git
- ARM存储系统的学习
- java jvm 内存划分
- 求解答No result defined for action myaction.LoginAction and result success错误
- Oracle undo表空间爆满的解决
- 关于Android项目导入AAR容易出现的坑
- iOS 银联支付开发流程