-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSumNumbers.java
More file actions
57 lines (51 loc) · 1.37 KB
/
SumNumbers.java
File metadata and controls
57 lines (51 loc) · 1.37 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
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
public class SumNumbers {
private int count = 0;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Integer[] arrI = null;
Vector<Integer> set = new Vector<Integer>();
int sum = 0;
void init(int[] array){
arrI = new Integer[array.length];
for(int i = 0; i < array.length; i++){
arrI[i] = new Integer(array[i]);
map.put(arrI[i], arrI[i] );
}
}
boolean isExist(Integer i){
return map.get(i) != null;
}
void comSum(int[] array, int start, int sum, int seti, boolean add){
if(add && seti > 1 && isExist(new Integer(sum))){
count++;
}
if(sum >= array[array.length -1] || start>= array.length)
return ;
comSum(array, start+1, sum + array[start], seti+1, true);
comSum(array, start+1, sum, seti, false);
}
void comSet(int [] array, int start){
if(start >= array.length){
count++;
return ;
}
sum += array[start];
comSet(array, start+1);
sum -= array[start];
comSet(array, start+1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {1, 2, 3, 4, 6};
int [] array1={3, 4,9,14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99};
SumNumbers sn = new SumNumbers();
sn.init(array1);
sn.comSum(array1, 0, 0, 0, false);
System.out.print(sn.count);
}
}