-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompEqual.java
More file actions
49 lines (46 loc) · 1.43 KB
/
Copy pathCompEqual.java
File metadata and controls
49 lines (46 loc) · 1.43 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
import java.util.*;
public class CombEqual{
public static void main(String[] args){
int target = 5;
int[] arr = {0,1,2,3,4,5};
List<List<Integer>> ans = new ArrayList<>();
find(target, arr, 0, ans, new ArrayList<Integer>());
for(List li : ans){
System.out.println(li);
}
System.out.println("==================");
List<Integer> testList = new ArrayList<>();
for(int i : arr){
testList.add(i);
}
List<List<Integer>> testAns = new ArrayList<>();
testAns.add(testList);
List<Integer> testAnss = new ArrayList<>(testList);
testList.add(9);
System.out.println(testAns);
System.out.println(testAnss);
}
public static void find(int target, int[] arr, int start, List<List<Integer>> ans, ArrayList<Integer> temp){
int total = sum(temp);
if(start > arr.length || total > target){
return;
}
if(target == total){
List<Integer> li = new ArrayList<>(temp);
ans.add(li);
return;
}
for(int i = start; i<arr.length; i++){
temp.add(arr[i]);
find(target, arr, i+1, ans, temp);
temp.remove(temp.size()-1);
}
}
public static int sum(List<Integer>temp){
int total = 0;
for(Integer i : temp){
total += i;
}
return total;
}
}