forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermutationsII.java
More file actions
39 lines (35 loc) · 1.1 KB
/
PermutationsII.java
File metadata and controls
39 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package LeetCode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//使用HashMap进行计数
public class PermutationsII {
private List<List<Integer>> res=new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums){
HashMap<Integer,Integer>count=new HashMap<>();
for(int i=0;i<nums.length;i++){
if(count.containsKey(nums[i])){
count.put(nums[i],count.get(nums[i])+1);
}else{
count.put(nums[i],1);
}
}
helper(nums,new ArrayList<>(),count);
return res;
}
public void helper(int[] nums,List<Integer> list,HashMap<Integer,Integer> count){
if(list.size()==nums.length){
res.add(new ArrayList<>(list));
return;
}
for(Integer i:count.keySet()){
if(count.get(i)>0){
list.add(i);
count.put(i,count.get(i)-1);
helper(nums,list,count);
count.put(i, count.get(i) + 1);
list.remove(list.size()-1);
}
}
}
}