【LeetCode】Permutations
2014-05-18 21:20
375 查看
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 ArrayList<ArrayList<Integer>> permute(int[] num) { ArrayList<ArrayList<Integer>> re =new ArrayList<ArrayList<Integer>>(); if(num.length==0) return re; ArrayList<Integer> ai = new ArrayList<Integer>(); ArrayList<ArrayList<Integer>> te =new ArrayList<ArrayList<Integer>>(); ai.add(num[0]); re.add(ai); te.add(ai); if(num.length==1) return re; for(int i=1;i<num.length;i++){ int t = num[i]; Iterator<ArrayList<Integer>> it = re.iterator(); te =new ArrayList<ArrayList<Integer>>(); while(it.hasNext()){ ai = it.next(); for(int j=0;j<ai.size();j++){ ai.add(j, t); ArrayList<Integer> tem = new ArrayList<Integer>(); for(int mm=0;mm<ai.size();mm++) tem.add(ai.get(mm)); te.add(tem); ai.remove(j); } ArrayList<Integer> ttem = new ArrayList<Integer>(); for(int nn=0;nn<ai.size();nn++) ttem.add(ai.get(nn)); ttem.add(t); te.add(ttem); } re=te; te=new ArrayList<ArrayList<Integer>>(); } return re; } }
相关文章推荐
- Permutations -- LeetCode
- LeetCode 046 Permutations
- 【leetcode】第46题 Permutations(递归法)题目+解析+代码
- leetcode:暴力枚举法之Permutations
- Permutations [LeetCode]
- Leetcode: Permutations
- LeetCode 45 Permutations
- leetcode - Permutations
- 算法Week08.04 - LeetCode 46. Permutations
- leetcode-Permutations(2014.2.3)
- [Leetcode]Permutations && Permutations II
- [LeetCode]Permutations
- leetcode[46]Permutations
- [LeetCode] Permutations
- leetcode -- Permutations
- [Leetcode] Permutations
- 【LeetCode】 046. Permutations
- LeetCode 046 Permutations
- [LeetCode]Permutations II@python
- LeetCode Permutations