46. Permutations
来源:互联网 发布:联通wcdma 通话 数据 编辑:程序博客网 时间:2024/06/16 04:33
求一个序列的所有排序组合。
题目
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]]
思路
深度搜索
递归实现
代码
//assert: distinct elements, no duplicatespublic class Solution { private IList<IList<int>> result = new List<IList<int>>(); public IList<IList<int>> Permute(int[] nums) { Array.Sort(nums); dfs(new List<int>(), nums); return result; } private void dfs(IList<int> tmp, int[] nums) { if (tmp.Count == nums.Length) { result.Add(new List<int>(tmp)); return; } for (int i = 0; i < nums.Length; i++) { if (tmp.Contains(nums[i])) continue; tmp.Add(nums[i]); dfs(tmp, nums); tmp.RemoveAt(tmp.Count - 1); } }}
阅读全文
2 0
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 前端优化-图片懒加载
- 动态代理和cglib的简单理解
- DNS可以有多个设置
- MVC 树节点Table格式授权
- Linux学习总结 unit09 网络管理
- 46. Permutations
- bootstraptable刷新数据
- HBase 无法访问60010端口
- vim操作-在查找模式中使用寄存器内容
- MVC 中的Model对象
- CentOS7 + Oracle12c 在注册监听时无效 遇到的问题及解决方法
- AndroidStudio安装配置教程
- 关于JSON拼接到前端页面进行展示
- 刷脸out了?阿里“刷脚购物”亮相CVPR 2017