-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathSubsets3.java
More file actions
35 lines (33 loc) · 1.11 KB
/
Subsets3.java
File metadata and controls
35 lines (33 loc) · 1.11 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
import java.util.ArrayList;
import java.util.List;
/**
* Subsets (Iterative)
* LeetCode: https://leetcode.com/problems/subsets/
*
* Approach:
* - Uses an iterative approach to generate all possible subsets.
* - Starts with an empty subset and iteratively adds each number to existing subsets to form new ones.
* - Efficiently builds the power set without recursion or backtracking.
*
* Time Complexity: O(N * 2^N)
* - There are 2^N possible subsets for an array of length N.
* - Each subset can take up to O(N) time to copy.
*
* Space Complexity: O(N * 2^N)
* - The result list contains 2^N subsets, each with up to N elements.
*/
public class Subsets3 {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
for(int i=0; i<nums.length ;i++){
int size = result.size();
for(int j=0; j<size ;j++){
List<Integer> list = new ArrayList<>(result.get(j));
list.add(nums[i]);
result.add(list);
}
}
return result;
}
}