46. Permutations
2016-03-07 17:16
288 查看
Permutations
题意很容易明白:
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], and [3,2,1].
代码(BackTracking):
public class Solution { List<List<Integer>> list; public List<List<Integer>> permute(int[] nums) { list = new ArrayList<>(); ArrayList<Integer> perm = new ArrayList<Integer>(); backTrack(perm,0,nums); return list; } void backTrack (ArrayList<Integer> perm,int i,int[] nums){ //Permutation completes if(i==nums.length){ list.add(new ArrayList(perm)); return; } ArrayList<Integer> newPerm = new ArrayList<Integer>(perm); //Insert elements in the array by increasing index for(int j=0;j<=i;j++){ newPerm.add(j,nums[i]); backTrack(newPerm,i+1,nums); newPerm.remove(j); } } }
原理:
1
12 21
312 132 123 321 231 213
相关文章推荐
- 打破现有市场壁垒、重建市场边界——老吴说产品
- 混编
- linux SSH免登录
- rsync 遇到过的问题
- bzoj3514
- MySQLdb模块 类操作方法
- Android中的缓存机制与实现
- 面向对象之开闭原则
- Ext入门学习系列(四)面板控件
- jmeter测试https
- MySQL索引设计原则
- Android Studio当中配置Open CV
- 多节点Kubernetes安装
- 关于类中方法的调用
- vim字符串替换
- Ext入门学习系列(五)表格控件(3)
- 【mark】App Widget设计
- jQuery 读取 JSONArray 的方法
- 求正整数各位数字之和
- centos 7 源码安装MySQL-5.6.20