Permutations II leetcode java

题目

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);
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     }

Permutations II leetcode java,布布扣,bubuko.com

时间: 2024-10-24 15:40:34

Permutations II leetcode java的相关文章

Pascal&#39;s Triangle II Leetcode java

题目: Given 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 use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Combination Sum II leetcode java

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

Reverse Linked List II leetcode java

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start =

Populating Next Right Pointers in Each Node II leetcode java

题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following bina

Unique Binary Search Trees II leetcode java

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:这道题比1难的就是不是返回个数,而

Path Sum II leetcode java

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题解: 这道题除了要判断是否有这样的一个p

Subset II leetcode java

题目: 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 solution