LeetCode: Permutations II 解题报告

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].

SOLUTION 1:

还是经典的递归模板。需要处理的情况是:我们先把Num排序,然后只能连续地选,这样就可以避免生成重复的solution.
例子:1 2 3 4 4 4 5 6 7 8
444这个的选法只能:4, 44, 444连续这三种选法

我们用一个visit的数组来记录哪些是选过的。

 1 public class Solution {
 2     public List<List<Integer>> permuteUnique(int[] num) {
 3         List<List<Integer>> ret = new ArrayList<List<Integer>>();
 4         if (num == null || num.length == 0) {
 5             return ret;
 6         }
 7
 8         // For deal with the duplicate solution, we should sort it.
 9         Arrays.sort(num);
10         boolean[] visit = new boolean[num.length];
11
12         dfs(num, new ArrayList<Integer>(), ret, visit);
13
14         return ret;
15     }
16
17     public void dfs(int[] num, ArrayList<Integer> path, List<List<Integer>> ret, boolean[] visit) {
18         int len = num.length;
19         if (path.size() == len) {
20             ret.add(new ArrayList<Integer>(path));
21             return;
22         }
23
24         for (int i = 0; i < len; i++) {
25             // 只能连续地选,这样就可以避免生成重复的solution.
26             // 例子:1 2 3 4 4 4 5 6 7 8
27             // 444这个的选法只能:4, 44, 444连续这三种选法
28             if (visit[i] || (i != 0 && visit[i - 1] && num[i] == num[i - 1])) {
29                 continue;
30             }
31
32             // 递归以及回溯
33             visit[i] = true;
34             path.add(num[i]);
35             dfs(num, path, ret, visit);
36             path.remove(path.size() - 1);
37             visit[i] = false;
38         }
39     }
40 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/permutation/PermuteUnique.java

时间: 2024-10-08 09:29:56

LeetCode: Permutations II 解题报告的相关文章

LeetCode: N-Queens II 解题报告

N-Queens II (LEVEL 4 难度级别,最高级5) Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 解答: 与第一题的解法是类似的,不同之处是,DFS的返回值是解的个数.我们每一次搜索本行8个位置时, 把8个位置的解都加在一起就行了.有的位置无解,它的返回值就是0咯. 另外注意BASE

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

LeetCode: Permutations II [046]

[题目] 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]. [题意] 给定一个候选数集合,候选集中可能存在重复数,返回所有的排列 [思路] 思路和Permutat

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

Pascal&#39;s Triangle &amp; II 解题报告

杨辉三角,分别求前n行和第n行. [求杨辉三角前n行] Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 基础题,直接看代码,注意边界. public class Solution { public List<List<Integer>&g

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,