Permutations II -- LeetCode

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                原题链接:  http://oj.leetcode.com/problems/permutations-ii/  
这个题跟 Permutations 非常类似,唯一的区别就是在这个题目中元素集合可以出现重复。这给我们带来一个问题就是如果不对重复元素加以区别,那么类似于{1,1,2}这样的例子我们会有重复结果出现。那么如何避免这种重复呢?方法就是对于重复的元素循环时跳过递归函数的调用,只对第一个未被使用的进行递归,我们那么这一次结果会出现在第一个的递归函数结果中,而后面重复的会被略过。如果第一个重复元素前面的元素还没在当前结果中,那么我们不需要进行递归。想明白了这一点,代码其实很好修改。首先我们要对元素集合排序,从而让重复元素相邻,接下来就是一行代码对于重复元素和前面元素使用情况的判断即可。代码如下: 
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();    if(num==null && num.length==0)        return res;    Arrays.sort(num);    helper(num, new boolean[num.length], new ArrayList<Integer>(), res);    return res;}private void helper(int[] num, boolean[] used, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> res){    if(item.size() == num.length)    {        res.add(new ArrayList<Integer>(item));        return;    }    for(int i=0;i<num.length;i++)    {        if(i>0 && !used[i-1] && num[i]==num[i-1]) continue;        if(!used[i])        {            used[i] = true;            item.add(num[i]);            helper(num, used, item, res);            item.remove(item.size()-1);            used[i] = false;        }    }}
这样的解法是带有一般性的,把这个代码放到 Permutations 中也是正确的,所以如果熟悉的话,面试时如果碰到这个题目建议直接实现这个代码,不要假设元素没有重复,当然可以跟面试官讨论,不过一般来说都是要考虑这个情况的哈。
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43418664/article/details/84137106