题目

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].

题解

这道题跟Permutaitons没啥大的区别,就是结果去重。

我之前也有写过去重的两个方法:

一个是在加入结果的时候用contains判断,一个是在找结果的时候看他是不是跟前一个元素相同。

这道题还要考虑的是visited情况,前一个元素就算跟当前元素相同,如果visited==true也没关系。但是如果前面元素跟当前元素相同还没被visited,那么就要做去重处理了。

代码如下:

 1     public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 3         ArrayList<Integer> item = new ArrayList<Integer>();
 4         
 5         if(num.length==0||num==null)
 6             return res;
 7         boolean[] visited = new boolean[num.length];  
 8         Arrays.sort(num);
 9         permutation_helper(num,res,item,visited);
         return res;
     }
     
     public void permutation_helper(int[] num, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> item,boolean[] visited){
         if(item.size()==num.length){
             res.add(new ArrayList<Integer>(item));
             return;
         }
         
         for(int i = 0; i<num.length;i++){
             if(i>0 && num[i-1] == num[i] && !visited[i-1])
                 continue;
             if(!visited[i]){
                 item.add(num[i]);
                 visited[i]=true;
                 permutation_helper(num,res,item,visited);
                 item.remove(item.size()-1);
                 visited[i]=false;
             }
         }
     }

实验:

如果未加visited判断的话,将会出现下面的错误:

Input: [1,1]
Output: []
Expected: [[1,1]]

因为执行了去重处理,所以一个结果都没有保留

同时,这里在每次添加遍历的item时候,没有判断该元素是否之前被visited过,这样同样会产生重复。

另外一个错误是,for循环的起始是start,而非每次从0开始,这样的话,会忽略掉start位置之前,未visited过的,非重复值。

比如: [1, 2],第一次记录结果[1,2]是正常的没有问题。 但是当退栈到第一个栈,走for循环时,item位置的第一个元素是2, 进入下一层递归,发现start位置是1(0+1),1位置上面是元素2,2被visited过了,所以就结束了这个程序。那么位置在0的,值为1的元素,就被忽略掉了。因为start位置没有从0开始,所以每次都应该从0位置开始。

错误代码如下:

 1     public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 3         ArrayList<Integer> item = new ArrayList<Integer>();
 4         
 5         if(num == null || num.length == 0)
 6             return res;
 7         //boolean [] visited = new boolean[num.length];
 8         Arrays.sort(num);
 9         permutationhelper(res, num, item, 0);
         return res;
     }
     
     public void permutationhelper(ArrayList<ArrayList<Integer>> res, int[] num, ArrayList<Integer> item, int start){
         if(item.size() == num.length){
             res.add(new ArrayList<Integer>(item));
             return;
         }
         
         for(int i = start; i < num.length; i++){
             if(i > 0 && num[i] == num[i-1])
                 continue;
             
             item.add(num[i]);
             permutationhelper(res, num, item, start+1);
             item.remove(item.size()-1);
         }
     }

为了更清楚的知道整个程序是如何运行的,代码中把start标记标出,正确代码如下:

 1     public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 3         ArrayList<Integer> item = new ArrayList<Integer>();
 4         
 5         if(num == null || num.length == 0)
 6             return res;
 7         boolean [] visited = new boolean[num.length];
 8         Arrays.sort(num);
 9         permutationhelper(res, num, item, visited, 0);
         return res;
     }
     
     public void permutationhelper(ArrayList<ArrayList<Integer>> res, int[] num, ArrayList<Integer> item, boolean[] visited, int start){
         if(item.size() == num.length){
             res.add(new ArrayList<Integer>(item));
             return;
         }
         
         for(int i = start; i < num.length; i++){
             if(i > 0 && num[i] == num[i-1] && !visited[i - 1])
                 continue;
             if(!visited[i]){
                 item.add(num[i]);
                 visited[i] = true;
                 permutationhelper(res, num, item, visited, start);
                 visited[i] = false;
                 item.remove(item.size()-1);
             }
         }
     }

Permutations II leetcode java的更多相关文章

  1. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

  2. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  3. Permutations II ——LeetCode

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  5. Word Break II leetcode java

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  6. Palindrome Partitioning II Leetcode java

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  7. Remove Duplicates from Sorted List II leetcode java

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...

  8. Ugly Number II leetcode java

    问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...

  9. Word Ladder II leetcode java

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

随机推荐

  1. java多线程快速入门(十一)

    在方法上面加synchonizd用的是this锁 package com.cppdy; class MyThread7 implements Runnable { private Integer ti ...

  2. php实现概率性随机抽奖代码

    1.初始数据: 权重越大,抽取的几率越高 [奖品1, 权重 5], [ 奖品2, 权重6], [ 奖品3, 权重 7], [ 奖品4, 权重2] 2.处理步骤: 1)N = 5 + 6 + 7 + 2 ...

  3. python 全栈开发,Day53(jQuery的介绍,jQuery的选择器,jQuery动画效果)

    js总结 js: 1.ECMAScript5 ES5语法 2.DOM CRUD 获取 3种方式 id tag className //面向对象 对象 : 属性和方法 某个对象中 function $( ...

  4. VS-常用的快捷键-总结

    1: 快速添加引用 === Shift+Alt+F10; 也可用于实现抽象类 2: 直接添加属性 ===prop+Tab+Tab; 3: 根据字段添加属性 === Ctrl +r+e; 4: 格式化代 ...

  5. each()遍历

    在<jQuery教程/理解选取更新范围>一节中,我们知道:当选择器返回了多个元素时,可以使用一个方法来更新所有的元素,不再需要使用循环. 然后有的时候需要遍历元素,怎么办? 使用each( ...

  6. python学习之集合

    集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典. 创建格 ...

  7. Python 时间获取

    摘自:http://www.jb51.net/article/91365.htm 摘自:https://www.cnblogs.com/liuq/p/6211005.html 一.在python中,除 ...

  8. java实现判断一个经纬度坐标是否在一个多边形内(经自己亲测)

    1.在高德地图上绘制的多边形:经纬度逗号分隔格式:上面是用来方便存坐标的对象:下面是方法测试:直接复制代码即可运行 public class Point { private Double x; pri ...

  9. 【Java】 剑指offer(35) 复杂链表的复制

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请实现函数ComplexListNode* Clone(Compl ...

  10. (转)HIBERNATE与 MYBATIS的对比

    第一方面:开发速度的对比 就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉得要用好Mybatis还是首先要先理解好H ...