Suppose my input array is [15,20,12] The required answer is a 2D array
The Required is output is as followed
[12
20
20 12
15
15 12
15 20
15 20 12
]
Here you go.
public static void main(String[] args) {
int[] nums= {15, 20, 12};
int[][] subsets = subsets(nums);
for (int i = 1; i < subsets.length; i++) {
System.out.println(Arrays.toString(subsets[i]));
}
}
public static int[][] subsets(int input[]) {
List<List<Integer>> list = new ArrayList<>();
subsetsHelper(list, new ArrayList<>(), input, 0);
return convertListToArray(list);
}
private static void subsetsHelper(List<List<Integer>> list , List<Integer> resultList, int [] nums, int start){
list.add(new ArrayList<>(resultList));
for(int i = start; i < nums.length; i++){
// add element
resultList.add(nums[i]);
// Explore
subsetsHelper(list, resultList, nums, i + 1);
// remove
resultList.remove(resultList.size() - 1);
}
}
private static int[][] convertListToArray(List<List<Integer>> list) {
int[][] array = new int[list.size()][];
for (int i = 0; i < array.length; i++) {
array[i] = new int[list.get(i).size()];
}
for(int i=0; i<list.size(); i++){
for (int j = 0; j < list.get(i).size(); j++) {
array[i][j] = list.get(i).get(j);
}
}
return array;
}
1.As each recursion call will represent subset here, add resultList(see recursion code above) to the list of subsets in each call. 2.Iterate over elements of a set. 3.In each iteration Add elements to the list explore(recursion) and make start = i+1 to go through remaining elements of the array. Remove element from the list
Output:
[15]
[15, 20]
[15, 20, 12]
[15, 12]
[20]
[20, 12]
[12]
Not clear if it's homework or practical case. This is how would I solve it using Guava PowerSet:
public static void main(String[] args) {
Integer input[] = {15,20,12};
List<Integer> rev = Lists.reverse(Arrays.asList(input));
Set<Integer> indices = IntStream.range(0, input.length).boxed().collect(ImmutableSet.toImmutableSet());
Object output[] = Sets.powerSet(indices).stream()
.filter(indexset -> !indexset.isEmpty())
.map(indexset -> indexset.stream().map(i -> rev.get(i)).collect(Collectors.collectingAndThen(toList(), Lists::reverse)))
.map(List::toArray)
.toArray();
System.out.println(Arrays.deepToString(output));
}
Disclaimer:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = { 15, 20, 12 };
System.out.println(Arrays.deepToString(subsets(arr)));
}
public static int[][] subsets(int input[]) {
int[][] subarrs = new int[(int) Math.pow(2, input.length) - 1][input.length];
int[] indices = { 0 };
subsetsHelper(input, subarrs, 0, 0, 0, indices);
return subarrs;
}
private static void subsetsHelper(int input[], int[][] subarrs, int index, int i, int j, int[] indices) {
if (i == input.length) {
subarrs[index] = input;
return;
}
int[] subarr = new int[indices.length];
for (int x = 0; x < subarr.length; x++) {
subarr[x] = input[indices[x]];
}
subarrs[index] = subarr;
if (j == input.length - 1) {
subsetsHelper(input, subarrs, index + 1, i + 1, i + 1, new int[] { i + 1 });
} else {
subsetsHelper(input, subarrs, index + 1, i, j + 1, new int[] { i, j + 1 });
}
}
}
Output:
[[15], [15, 20], [15, 12], [20], [20, 12], [12], [15, 20, 12]]
public static int[][]returnallsub(int arr[], int si){
if(si==arr.length)
{int[][]ret=new int [1][0];
return ret;
}
int [][]rss =returnallsub(arr,si+1);
int [][]tss=new int[rss.length*2][];
int i=0;
for(;i<rss.length;i++) {
tss[i]=new int [rss[i].length];
}
int k=0;
for(;k<rss.length;k++) {
tss[i]=new int [rss[k].length+1];
i++;
}
int j=0;
for(i=0;i<rss.length;i++) {
for(j=0;j<rss[i].length;j++){
tss[i][j]=rss[i][j];
}
}
for(k=i;k<tss.length;k++) {
tss[k][0]=arr[si];
}
int r=i;
for(i=0;i<rss.length;i++) {
for(j=0;j<rss[i].length;j++){
tss[r][j+1]=rss[i][j];
}
r++;
}
return tss;
}
public static int[][] subsets(int arr[]) {
int start=0;
return returnallsub( arr,0);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.