LintCode 15-全排列 16-带重复元素的全排列

本人电子系,只为一学生。心喜计算机,小编以怡情。
给定一个数字列表,返回其所有可能的排列。
全排列
注意事项
你可以假设没有重复数字。
样例
给出一个列表[1,2,3],其全排列为:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

【LintCode 15-全排列 16-带重复元素的全排列】注:15题与16题解法是相同的,只要把函数名permute<—>permuteUnique
改一改,递归子程序不变,两个题是完全适用的
static public List> permute(int[] nums) { //定义了两个用于递归的参数 List> ret=new ArrayList<>(); List temp= new ArrayList<>(); //定义了一个标记是否访问过的数组 int []visited=new int[nums.length]; //参数:(层数k,总层数n,ret,temp,数组候选解,访问标志) recursion(0,nums.length,ret, temp, nums,visited); return ret; } static public void recursion(int k,int n,List> ret,List temp,int []nums,int []visited) { //如果temp有n个元素,ret中没有 if(k==n&&!ret.contains(temp)) ret.add(new ArrayList(temp)); //ret就添加if(k>n)//退出条件限制递归层数 return; else { for(int i=0; i

    推荐阅读