zoukankan      html  css  js  c++  java
  • LeetCode 47. Permutations II

    原题链接在这里:https://leetcode.com/problems/permutations-ii/

    题目:

    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]

    题解:

    nums中有重复元素,为了避免加重复值,需要去重.

    去重的方法有两种,一个是加结果之前用contains看是否有重复结果,另外一个是找结果之前比较它是否与前一个元素相同。

    Recursion方法和Permutations相同,但为了去重,不但需要比较与之前元素是否相同,还需要看之前元素是否被use过, 如果usd[i-1] = true 说明前个相同的值已经在item里, 那么没有关系.

    但若之前元素没有被用过那么就需要跳过.

    Time Complexity: exponenetial.

    Space: O(nums.length). stack space. regardless res.

    AC Java:

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

    Iteration方法和Subsets II很像,加进newRes之前检查newRes是否包含了重复item, 没有重复才可以加进来.

    Time Complexity: exponential. Space: O(res.size()).

    AC Java:

     1 public class Solution {
     2     public List<List<Integer>> permuteUnique(int[] nums) {
     3         List<List<Integer>> res = new ArrayList<List<Integer>>();
     4         if(nums == null || nums.length == 0){
     5             return res;
     6         }
     7         List<Integer> item = new ArrayList<Integer>();
     8         item.add(nums[0]);
     9         res.add(item);
    10         for(int i = 1; i<nums.length; i++){
    11             List<List<Integer>> newRes = new ArrayList<List<Integer>>();
    12             for(int j = 0; j<res.size(); j++){
    13                 List<Integer> cur = res.get(j);
    14                 for(int k=0;k<cur.size()+1;k++){
    15                     item = new ArrayList<Integer>(cur);
    16                     item.add(k,nums[i]);
    17                     if(!newRes.contains(item)){
    18                         newRes.add(item);
    19                     }
    20                 }
    21             }
    22             res = newRes;
    23         }
    24         return res;
    25     }
    26 }

    类似Permutations

  • 相关阅读:
    [SNOI2019]数论
    2018-8-10-C#-写系统日志
    2018-8-10-C#-写系统日志
    2019-3-1-C#-double-好用的扩展
    2019-3-1-C#-double-好用的扩展
    2019-8-31-dotnet-Framework-源代码-·-Ink
    2019-8-31-dotnet-Framework-源代码-·-Ink
    2019-8-31-How-to-fix-nuget-Unrecognized-license-type-MIT-when-pack
    2019-8-31-How-to-fix-nuget-Unrecognized-license-type-MIT-when-pack
    2018-9-30-C#-传入-params-object-长度
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4842119.html
Copyright © 2011-2022 走看看