<leetcode> Permutations
来源:互联网 发布:java学籍管理系统代码 编辑:程序博客网 时间:2024/05/16 17:50
Given a collection of 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]
.
思路同Combination, 题目要求没有重复元素,所以建立一个arraylist标志每一个元素,如果已经选用设为1。
同样,需要每次递归前克隆一个list
public class Solution { public List<List<Integer>> permute(int[] num) { List<List<Integer>> result=new ArrayList<List<Integer>>(); if(num==null||num.length<=0){ return result; } ArrayList<Integer> solution=new ArrayList<Integer>(); ArrayList<Integer> flag=new ArrayList<Integer>(); for(int i=0;i<num.length;i++){ flag.add(0); } getPermute(num,result,solution,flag); return result; } public void getPermute(int[] num, List<List<Integer>> result, ArrayList<Integer> solution, ArrayList<Integer> flag){ if(solution.size()==num.length){ result.add(solution); return; } else{ for(int i=0;i<num.length;i++){ if(flag.get(i)==0){ ArrayList<Integer> solutionCopy=(ArrayList)solution.clone(); ArrayList<Integer> flagCopy=(ArrayList)flag.clone(); solutionCopy.add(num[i]); flagCopy.set(i,1); getPermute(num,result,solutionCopy,flagCopy); } } } }}
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- [LeetCode] Permutations
- Leetcode: Permutations
- [Leetcode] Permutations
- [Leetcode] Permutations
- [LeetCode]Permutations
- LeetCode-Permutations
- [leetcode] permutations
- LeetCode - Permutations
- Leetcode: Permutations
- 【leetcode】Permutations
- 【LeetCode】Permutations
- cocos2d-x进化为2.5D的一些想法
- Regular Expression
- slurm简介
- 柳珩 朝射暮讀
- 執觴請換 表憲
- <leetcode> Permutations
- 不懂语言代码,超级菜鸟的建站分享(二):界面设置
- 优化工具 Neos Server
- IntelliJ IDEA快捷键
- easyUI学习记录(一)- Helloword&&easyloader介绍
- LoadRunner代码模拟参数化过程
- Wildcard Matching
- LeetCode总结 -- 图篇
- 第六章、Linux 的文件权限与目录配置