The trick here is to name the state correctly, then let the implementation follow. For 78. Subsets, the solution in this repository is mainly a bit manipulation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: bit manipulation, backtracking.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are backtrack.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N*2^N), space: O(N*2^N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 97.95% of C++ online submissions for Subsets.
02//Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Subsets.
03class Solution {
04public:
05 void backtrack(vector<vector<int>>& ans, vector<int>& subset, vector<int>& nums, int k){
06 if(subset.size() == k){
07 ans.push_back(subset);
08 }else{
09 //set "start" so that the elements in subset are in ascending order
10 int start = 0;
11 if(subset.size() > 0){
12 //start from the next element of subset[subset.size()-1]
13 start = find(nums.begin(), nums.end(), subset[subset.size()-1]) - nums.begin() + 1;
14 }
15 // for(int e : nums){
16 // for(int i = subset.size(); i < nums.size(); i++){
17 for(int i = start; i < nums.size(); i++){
18 int e = nums[i];
19 // cout << start << " " << i << endl;
20 subset.push_back(e);
21 backtrack(ans, subset, nums, k);
22 subset.pop_back();
23 }
24 }
25 };
26
27 vector<vector<int>> subsets(vector<int>& nums) {
28 vector<vector<int>> ans;
29 vector<int> subset;
30
31 //from empty set to itself
32 for(int k = 0; k <= nums.size(); k++){
33 backtrack(ans, subset, nums, k);
34 }
35 return ans;
36 }
37};
38
39//Approach 2: Backtracking
40//time: O(N*2^N), space: O(N*2^N)
41//https://leetcode.com/problems/permutations/discuss/18239/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partioning)
42//Runtime: 4 ms, faster than 97.95% of C++ online submissions for Subsets.
43//Memory Usage: 8.2 MB, less than 100.00% of C++ online submissions for Subsets.
44class Solution {
45public:
46 void backtrack(vector<vector<int>>& ans, vector<int>& subset, vector<int>& nums, int start){
47 ans.push_back(subset);
48 for(int i = start; i < nums.size(); i++){
49 subset.push_back(nums[i]);
50 backtrack(ans, subset, nums, i+1);
51 subset.pop_back();
52 }
53 };
54
55 vector<vector<int>> subsets(vector<int>& nums) {
56 vector<vector<int>> ans;
57 vector<int> subset;
58
59 backtrack(ans, subset, nums, 0);
60
61 return ans;
62 }
63};
64
65//Approach 1: Recursion
66//time: O(N*2^N), space: O(N*2^N)
67//Runtime: 4 ms, faster than 97.95% of C++ online submissions for Subsets.
68//Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Subsets.
69class Solution {
70public:
71 vector<vector<int>> subsets(vector<int>& nums) {
72 vector<vector<int>> ans = {vector<int>()};
73
74 for(int num : nums){
75 // cout << "num: " << num << endl;
76 int ans_size = ans.size();
77 // for(vector<int>& subset : ans){
78 for(int i = 0; i < ans_size; i++){
79 vector<int> cur = ans[i];
80 // cout << "subset size: " << cur.size() << endl;
81 cur.push_back(num);
82 ans.push_back(cur);
83 // cout << "ans size: " << ans.size() << endl;
84 }
85 }
86
87 return ans;
88 }
89};
90
91//Approach 3: Lexicographic (Binary Sorted) Subsets
92//time: O(N*2^N), space: O(N*2^N)
93//Runtime: 8 ms, faster than 53.38% of C++ online submissions for Subsets.
94//Memory Usage: 8.4 MB, less than 100.00% of C++ online submissions for Subsets.
95class Solution {
96public:
97 vector<vector<int>> subsets(vector<int>& nums) {
98 //its binary representation has length (n+1)
99 int N = nums.size();
100 int nthBit = 1 << N;
101 vector<vector<int>> ans;
102
103 // cout << N << " " << nthBit << endl;
104
105 //(1 << n) equals to (int)pow(2, n)
106 for(int i = 0; i < nthBit; i++){
107 const size_t bitcount = sizeof(nthBit)*8;
108 string binary = bitset<bitcount>(i | nthBit).to_string();
109 //the first (bitcount -N) bit is just used for keeping the leading 0, so here ignore it
110 //select the last N bits
111 string bitmask = binary.substr(bitcount-N);
112
113 // cout << i << " " << bitmask << endl;
114
115 vector<int> subset;
116 for(int j = 0; j < N; j++){
117 if(bitmask[j] == '1'){
118 subset.push_back(nums[j]);
119 }
120 }
121 ans.push_back(subset);
122 }
123
124 return ans;
125 }
126};
Cost