Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Subset {
- public static void main(String[] args) {
- Subset sub = new Subset();
- int[] arr = new int[] { 1, 2, 3 };
- sub.subsets(arr);
- }
- List<List<Integer>> opList = new ArrayList<>();
- public List<List<Integer>> subsets(int[] nums) {
- int temp[] = new int[nums.length];
- printAll(nums, nums.length, 0, temp, 0);
- return opList;
- }
- public void printAll(int[] arr, int n, int i, int[] temp, int s) {
- if (i == n) {
- List<Integer> minList = new ArrayList<>();
- for (int j = 0; j < s; j++) {
- minList.add(temp[j]);
- }
- opList.add(minList);
- return;
- }
- temp[s] = arr[i];
- printAll(arr, n, i + 1, temp, s + 1); // yes
- printAll(arr, n, i + 1, temp, s);// no
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement