-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllSortK.java
More file actions
37 lines (30 loc) · 1.02 KB
/
AllSortK.java
File metadata and controls
37 lines (30 loc) · 1.02 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
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class AllSortK {
public static void main(String[] args){
int[] num = new int[]{1,2,3};
List<List<Integer>> out= sort(num);
System.out.println(out);
}
public static List<List<Integer>> sort(int[] nums) {
List<Integer> list = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
util(result,list,nums, 0);
for(int a : nums) list.add(a);
result.add(list);
return result;
}
public static void util(List<List<Integer>> result,List<Integer> list,int[] num, int i) {
for(int j = i; j < num.length; j++){
list.add(num[j]);
if(!result.contains(list) && list.size() != num.length) {
result.add(new ArrayList<>(list));
}
util(result, list, num, j+1 );
list.remove(list.size() - 1);
} return;
}
}