题目

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.  1     public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
  2.  2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
  3.  3         ArrayList<Integer> item = new ArrayList<Integer>();
  4.  4         
  5.  5         if(num.length==0||num==null)
  6.  6             return res;
  7.  7         boolean[] visited = new boolean[num.length];  
  8.  8         Arrays.sort(num);
  9.  9         permutation_helper(num,res,item,visited);
  10.          return res;
  11.      }
  12.      
  13.      public void permutation_helper(int[] num, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> item,boolean[] visited){
  14.          if(item.size()==num.length){
  15.              res.add(new ArrayList<Integer>(item));
  16.              return;
  17.          }
  18.          
  19.          for(int i = 0; i<num.length;i++){
  20.              if(i>0 && num[i-1] == num[i] && !visited[i-1])
  21.                  continue;
  22.              if(!visited[i]){
  23.                  item.add(num[i]);
  24.                  visited[i]=true;
  25.                  permutation_helper(num,res,item,visited);
  26.                  item.remove(item.size()-1);
  27.                  visited[i]=false;
  28.              }
  29.          }
  30.      }

实验:

如果未加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.  1     public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
  2.  2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
  3.  3         ArrayList<Integer> item = new ArrayList<Integer>();
  4.  4         
  5.  5         if(num == null || num.length == 0)
  6.  6             return res;
  7.  7         //boolean [] visited = new boolean[num.length];
  8.  8         Arrays.sort(num);
  9.  9         permutationhelper(res, num, item, 0);
  10.          return res;
  11.      }
  12.      
  13.      public void permutationhelper(ArrayList<ArrayList<Integer>> res, int[] num, ArrayList<Integer> item, int start){
  14.          if(item.size() == num.length){
  15.              res.add(new ArrayList<Integer>(item));
  16.              return;
  17.          }
  18.          
  19.          for(int i = start; i < num.length; i++){
  20.              if(> 0 && num[i] == num[i-1])
  21.                  continue;
  22.              
  23.              item.add(num[i]);
  24.              permutationhelper(res, num, item, start+1);
  25.              item.remove(item.size()-1);
  26.          }
  27.      }

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

  1.  1     public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
  2.  2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
  3.  3         ArrayList<Integer> item = new ArrayList<Integer>();
  4.  4         
  5.  5         if(num == null || num.length == 0)
  6.  6             return res;
  7.  7         boolean [] visited = new boolean[num.length];
  8.  8         Arrays.sort(num);
  9.  9         permutationhelper(res, num, item, visited, 0);
  10.          return res;
  11.      }
  12.      
  13.      public void permutationhelper(ArrayList<ArrayList<Integer>> res, int[] num, ArrayList<Integer> item, boolean[] visited, int start){
  14.          if(item.size() == num.length){
  15.              res.add(new ArrayList<Integer>(item));
  16.              return;
  17.          }
  18.          
  19.          for(int i = start; i < num.length; i++){
  20.              if(> 0 && num[i] == num[i-1] && !visited[- 1])
  21.                  continue;
  22.              if(!visited[i]){
  23.                  item.add(num[i]);
  24.                  visited[i] = true;
  25.                  permutationhelper(res, num, item, visited, start);
  26.                  visited[i] = false;
  27.                  item.remove(item.size()-1);
  28.              }
  29.          }
  30.      }

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. vue首次赋值不触发watch

    可通过其immediate 属性进行配置,默认为false watch:{ "aaa":{ immediate:true, handler:function(){ } }

  2. Array数组内函数

      concat() 功能:合并数组,并且生成新数组.对原数组没有改变. 不传参数的时候,相当于生成新数组. 格式:数组.concat(数据...数组); 返回值:生成的新数组 代码示例: //.co ...

  3. python 全栈开发,Day67(Django简介)

    昨日内容回顾 1. socket创建服务器 2. http协议: 请求协议 请求首行 请求方式 url?a=1&b=2 协议 请求头 key:value 请求体 a=1&b=2(只有p ...

  4. apache tomcat 集群!

    公司需要一个内部测试局域网, 要求可以支持3000并发访问!以前也没做过服务器这方面.临时抱佛脚,查看了N多文档,他人经验,布置好之后,又遇到了N多问题,功夫不负有心人.终于还是完成了要求!观他人的布 ...

  5. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  6. android修改默认输入法

    方案一:adb命令设置?方案2:系统配置:方案3:调用系统API接口设置---------------------------adb shell cmdadb rootadb remountadb p ...

  7. SQL存储过程使用参考代码

    存储过程   use EBuy go  --常用的系统存储过程  sp_addmessage  --将新的用户定义错误消息存储在SQL Server数据库实例中  sp_helptext  --显示用 ...

  8. MAC OS X下配置PHP开发、调试环境

    操作系统:MAC OS X 工具:MAMP.PhpStorm.xdebug.chrome 1.下载MAMP 2.安装比较简单,安装完成后,应用程序中会增加如下4个应用 MacGDBp是PHP调试器,使 ...

  9. 030.Zabbix分布式部署

    一 分布式Zabbix介绍 zabbix proxy 可以代替 zabbix server 收集性能和可用性数据,然后把数据汇报给 zabbix server,并且在一定程度上分担了zabbix se ...

  10. python新手总结(二)

    random模块 随机小数 random uniform 随机整数 randint randrange 随机抽取 choice sample 打乱顺序 shuffle random.random() ...