-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubSetII.java
More file actions
92 lines (78 loc) · 1.62 KB
/
SubSetII.java
File metadata and controls
92 lines (78 loc) · 1.62 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.*;
import java.util.Map.Entry;
public class SubSetII {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public List<List<Integer>> subsetsWithDup(int[] num) {
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
for(int i=0; i<num.length; i++)
{
if(!map.containsKey(num[i]))
{
map.put(num[i], 1);
}
else
{
map.put(num[i], map.get(num[i])+1);
}
}
int[] digits = new int[map.size()];
int[] upbounds = new int[map.size()];
int[] changingNumber= new int[upbounds.length];
Iterator<Entry<Integer, Integer>> it = map.entrySet().iterator();
int i=0;
while(it.hasNext())
{
Entry<Integer, Integer> pairs = it.next();
digits[i] = pairs.getKey();
upbounds[i] = pairs.getValue();
changingNumber[i]=0;
i++;
}
List<List<Integer>> lists = new ArrayList<List<Integer>>();
do
{
List<Integer> list = new ArrayList<Integer>();
for(int k =0 ;k<digits.length; k++)
{
for(int j=0; j<changingNumber[k]; j++)
{
list.add(digits[k]);
}
}
lists.add(list);
}
while(!Increment(upbounds, changingNumber));
return lists;
}
// Overflow or not
boolean Increment(int[] upbounds, int[] changingNumber)
{
int overflow = 1;
int i=0;
while(i< changingNumber.length)
{
if(overflow > 0)
{
int sum = changingNumber[i] + overflow;
if(sum > upbounds[i])
{
overflow = 1;
changingNumber[i] = 0;
i++;
}
else
{
overflow = 0;
changingNumber[i] = sum;
break;
}
}
}
return overflow==1;
}
}