leetcode Permutation
2015-10-29 14:16
141 查看
Given a collection of numbers, return all possible permutations.
For example,
求全排列,相当于回溯法中的排列树。
For example,
[1,2,3]have the following permutations:
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2], and
[3,2,1].
求全排列,相当于回溯法中的排列树。
public class Solution { public List<List<Integer>> permute(int[] nums) { HashSet<List<Integer>> res = new HashSet<List<Integer>>(); backtrace(0,nums.length,nums,res); return new ArrayList(res); } public void backtrace(int t,int n,int[] nums,HashSet<List<Integer>> res){ if(t>=n){ List<Integer> list = new ArrayList<Integer>(); for(int i = 0;i<n;i++) list.add(nums[i]); res.add(list); } else{ for(int i = t ;i<n; i++){ swap(nums,i,t); backtrace(t+1, n, nums, res); swap(nums,t,i); } } } public int[] swap(int[] ints, int x, int y) { int temp = ints[x]; ints[x] = ints[y]; ints[y] = temp; return ints; } }
相关文章推荐
- VBS教程:创建txt文件
- kvm(libivrt)bridge和nat模式共存互通
- Spring学习1--基本概念
- 14:05:15,181 ERROR PARSER:56 - <AST>:0:0: unexpected end of subtree
- android中像素单位dp、px、pt、sp的比较
- Eclipse几个版本号的区别
- 验证Oracle处理速度
- 判断checkbox是否被选中事件
- 近期面试遇到的问题以及总结(python和openstack)
- 让微信二维码扫描您的APK
- WPF分页控件
- oracle 开发中用到的函数总结
- 用CSDN Markdown在线编辑器写博客
- 二维树状数组
- spark-streaming系列------- 4. Spark-Streaming Job的生成和执行
- jQuery与ajax 基础运用
- textView输入字数统计
- 实现TextView的倒影效果
- Linux基础一cd 与 ls 命令详解
- CAS单点登录(SSO)完整教程