|
| 1 | +# 题目描述(中等难度) |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +和[上一道题](https://leetcode.windliang.cc/leetCode-46-Permutations.html)类似,不同之处就是给定的数字中会有重复的,这样的话用之前的算法会产出重复的序列。例如,[ 1 1 ],用之前的算法,产生的结果肯定是 [ \[ 1 1 \], \[ 1 1 \] ],也就是产生了重复的序列。但我们可以在上一题的解法中进行修改从而解决这道题。 |
| 6 | + |
| 7 | +# 解法一 插入 |
| 8 | + |
| 9 | +这个没想到怎么在原基础上改,可以直接了当些,在它产生的结果里,对结果去重再返回。对于去重的话,一般的方法肯定就是写两个 for 循环,然后一个一个互相比较,然后找到重复的去掉。这里,我们用 [39题](https://leetcode.windliang.cc/leetCode-39-Combination-Sum.html?h=remove) 解法二中提到的一种去重的方法。 |
| 10 | + |
| 11 | +```java |
| 12 | +public List<List<Integer>> permuteUnique(int[] nums) { |
| 13 | + List<List<Integer>> all = new ArrayList<>(); |
| 14 | + List<Integer> temp = new ArrayList<>(); |
| 15 | + temp.add(nums[0]); |
| 16 | + all.add(temp); |
| 17 | + for (int i = 1; i < nums.length; i++) { |
| 18 | + int current_size = all.size(); |
| 19 | + for (int j = 0; j < current_size; j++) { |
| 20 | + List<Integer> last = all.get(j); |
| 21 | + for (int k = 0; k <= i; k++) { |
| 22 | + if (k < i && nums[i] == last.get(k)) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + temp = new ArrayList<>(last); |
| 26 | + temp.add(k, nums[i]); |
| 27 | + all.add(temp); |
| 28 | + } |
| 29 | + } |
| 30 | + for (int j = 0; j < current_size; j++) { |
| 31 | + all.remove(0); |
| 32 | + } |
| 33 | + } |
| 34 | + return removeDuplicate(all); |
| 35 | +} |
| 36 | + |
| 37 | +private List<List<Integer>> removeDuplicate(List<List<Integer>> list) { |
| 38 | + Map<String, String> ans = new HashMap<String, String>(); |
| 39 | + for (int i = 0; i < list.size(); i++) { |
| 40 | + List<Integer> l = list.get(i); |
| 41 | + String key = ""; |
| 42 | + // [ 2 3 4 ] 转为 "2,3,4" |
| 43 | + for (int j = 0; j < l.size() - 1; j++) { |
| 44 | + key = key + l.get(j) + ","; |
| 45 | + } |
| 46 | + key = key + l.get(l.size() - 1); |
| 47 | + ans.put(key, ""); |
| 48 | + } |
| 49 | + // 根据逗号还原 List |
| 50 | + List<List<Integer>> ans_list = new ArrayList<List<Integer>>(); |
| 51 | + for (String k : ans.keySet()) { |
| 52 | + String[] l = k.split(","); |
| 53 | + List<Integer> temp = new ArrayList<Integer>(); |
| 54 | + for (int i = 0; i < l.length; i++) { |
| 55 | + int c = Integer.parseInt(l[i]); |
| 56 | + temp.add(c); |
| 57 | + } |
| 58 | + ans_list.add(temp); |
| 59 | + } |
| 60 | + return ans_list; |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +# 解法二 回溯 |
| 65 | + |
| 66 | +看下之前的算法 |
| 67 | + |
| 68 | +```java |
| 69 | +public List<List<Integer>> permute(int[] nums) { |
| 70 | + List<List<Integer>> list = new ArrayList<>(); |
| 71 | + backtrack(list, new ArrayList<>(), nums); |
| 72 | + return list; |
| 73 | +} |
| 74 | + |
| 75 | +private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){ |
| 76 | + if(tempList.size() == nums.length){ |
| 77 | + list.add(new ArrayList<>(tempList)); |
| 78 | + } else{ |
| 79 | + for(int i = 0; i < nums.length; i++){ |
| 80 | + if(tempList.contains(nums[i])) continue; // 已经存在的元素,跳过 |
| 81 | + tempList.add(nums[i]); //将当前元素加入 |
| 82 | + backtrack(list, tempList, nums); //向后继续添加 |
| 83 | + tempList.remove(tempList.size() - 1); //将 tempList 刚添加的元素,去掉,尝试新的元素 |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +假如给定的数组是 [ 1 1 3 ],我们来看一下遍历的这个图。 |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +第一个要解决的就是这句代码 |
| 94 | + |
| 95 | +```java |
| 96 | +if(tempList.contains(nums[i])) continue; // 已经存在的元素,跳过 |
| 97 | +``` |
| 98 | + |
| 99 | +之前没有重复的元素,所以可以直接在 templist 判断有没有当前元素,有的话就跳过。但这里的话,因为给定的有重复的元素,这个方法明显不可以了。 |
| 100 | + |
| 101 | +换个思路,我们可以再用一个 list 保存当前 templist 中已经有的元素的下标,然后添加新元素的时候去判断下标就可以了。 |
| 102 | + |
| 103 | +第二个问题就是,可以看到有重复元素的时候,上边第 1 个图和第 2 个图产生的是完全一样的序列。所以第 2 个遍历是没有必要的。 |
| 104 | + |
| 105 | +解决的方案就是把数组首先排下顺序,然后判断一下上一个添加的元素和当前元素是不是相等,相等的话就跳过,继续下一个元素。 |
| 106 | + |
| 107 | +```java |
| 108 | +public List<List<Integer>> permuteUnique(int[] nums) { |
| 109 | + List<List<Integer>> list = new ArrayList<>(); |
| 110 | + Arrays.sort(nums); |
| 111 | + List<Integer> old = new ArrayList<>(); |
| 112 | + backtrack(list, new ArrayList<>(), nums, old); |
| 113 | + return list; |
| 114 | +} |
| 115 | + |
| 116 | +private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, List<Integer> old) { |
| 117 | + if (tempList.size() == nums.length) { |
| 118 | + list.add(new ArrayList<>(tempList)); |
| 119 | + } else { |
| 120 | + for (int i = 0; i < nums.length; i++) { |
| 121 | + //解决第一个问题 |
| 122 | + if (old.contains(i)) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + //解决第二个问题 !old.contains(i - 1) 很关键,下边解释下 |
| 126 | + if (i > 0 && !old.contains(i - 1) && nums[i - 1] == nums[i]) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + old.add(i);//添加下标 |
| 130 | + tempList.add(nums[i]); // 将当前元素加入 |
| 131 | + backtrack(list, tempList, nums, old); // 向后继续添加 |
| 132 | + old.remove(old.size() - 1); |
| 133 | + tempList.remove(tempList.size() - 1); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +解决第二个问题 !old.contains(i - 1) 很关键 |
| 140 | +因为上边 old.contains(i) 代码会使得一些元素跳过没有加到 templist 上,所以我们要判断 nums[ i - 1 ] 是不是被跳过的那个元素,如果 old.contains ( i ) 返回 true , 即使 nums [ i - 1 ] == nums [ i ] 也不能跳过当前元素。因为上一个元素 nums [ i - 1 ] 并没有被添加到 templist。可能比较绕,但是可以参照上边的图,走一下流程就懂了。如果不加 !old.contains ( i - 1 ),那么图中的第 2 行的第 2 个 1 本来应该加到 tempList,但是会被跳过。因为第 2 行第 1 个元素也是 1。 |
| 141 | + |
| 142 | +对于解决第一个问题,我们用了一个 list 来保存下标来解决。需要一个 O ( n ) 的空间。有一种方法,我们可以用 O(1)的空间。不过前提是,我们需要对问题的样例了解,也就是给定的输入所包含的数字。我们需要找到一个样例中一定不包含的数字来解决我们的问题。 |
| 143 | + |
| 144 | +首先,我们假设输入的所有的数字中没有 -100 这个数字。 |
| 145 | + |
| 146 | +然后,我们就可以递归前将当前数字先保存起来,然后置为 -100 隐藏起来,递归结束后还原即可。 |
| 147 | + |
| 148 | +```java |
| 149 | +public List<List<Integer>> permuteUnique(int[] nums) { |
| 150 | + List<List<Integer>> list = new ArrayList<>(); |
| 151 | + Arrays.sort(nums); |
| 152 | + backtrack(list, new ArrayList<>(), nums); |
| 153 | + return list; |
| 154 | +} |
| 155 | + |
| 156 | +private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums) { |
| 157 | + if (tempList.size() == nums.length) { |
| 158 | + list.add(new ArrayList<>(tempList)); |
| 159 | + } else { |
| 160 | + for (int i = 0; i < nums.length; i++) { |
| 161 | + //解决第一个问题 |
| 162 | + if (nums[i] == -100) { |
| 163 | + continue; |
| 164 | + } |
| 165 | + //解决第二个问题 !old.contains(i - 1) 很关键 |
| 166 | + if (i > 0 && nums[i-1] != -100 && nums[i - 1] == nums[i]) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + tempList.add(nums[i]); // 将当前元素加入 |
| 170 | + int temp = nums[i]; //保存 |
| 171 | + nums[i] = -100; // 隐藏 |
| 172 | + backtrack(list, tempList, nums); // 向后继续添加 |
| 173 | + nums[i] = temp; //还原 |
| 174 | + tempList.remove(tempList.size() - 1); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +当然这个想法局限性很大,但是如果对解决的问题很熟悉,一般是可以找到这样一个不会输入的数字,然后可以优化空间复杂度。 |
| 183 | + |
| 184 | +# 解法三 交换 |
| 185 | + |
| 186 | +这个改起来相对容易些,之前的想法就是在每一个位置,让每个数字轮流交换过去一下。这里的话,我们其实只要把当前位置已经有哪些数字来过保存起来,如果有重复的话,我们不让他交换,直接换下一个数字就可以了。 |
| 187 | + |
| 188 | +```java |
| 189 | +public List<List<Integer>> permuteUnique(int[] nums) { |
| 190 | + List<List<Integer>> all = new ArrayList<>(); |
| 191 | + Arrays.sort(nums); |
| 192 | + upset(nums, 0, all); |
| 193 | + return all; |
| 194 | +} |
| 195 | + |
| 196 | +private void upset(int[] nums, int begin, List<List<Integer>> all) { |
| 197 | + if (begin == nums.length) { |
| 198 | + ArrayList<Integer> temp = new ArrayList<Integer>(); |
| 199 | + for (int i = 0; i < nums.length; i++) { |
| 200 | + temp.add(nums[i]); |
| 201 | + } |
| 202 | + all.add(new ArrayList<Integer>(temp)); |
| 203 | + return; |
| 204 | + } |
| 205 | + HashSet<Integer> set = new HashSet<>(); //保存当前要交换的位置已经有过哪些数字了 |
| 206 | + for (int i = begin; i < nums.length; i++) { |
| 207 | + if (set.contains(nums[i])) { //如果存在了就跳过,不去交换 |
| 208 | + continue; |
| 209 | + } |
| 210 | + set.add(nums[i]); |
| 211 | + swap(nums, i, begin); |
| 212 | + upset(nums, begin + 1, all); |
| 213 | + swap(nums, i, begin); |
| 214 | + } |
| 215 | + |
| 216 | +} |
| 217 | + |
| 218 | +private void swap(int[] nums, int i, int begin) { |
| 219 | + int temp = nums[i]; |
| 220 | + nums[i] = nums[begin]; |
| 221 | + nums[begin] = temp; |
| 222 | +} |
| 223 | +``` |
| 224 | + |
| 225 | +# 总 |
| 226 | + |
| 227 | +基本上都是在上道题的基础上改出来了,一些技巧也是经常遇到,比如先排序,然后判断和前一个是否重复。利用 Hash 去重的功能。利用原来的存储空间隐藏掉数据,然后再想办法还原。 |
0 commit comments