Skip to content

Commit

Permalink
some leetcode, we're back
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored May 21, 2024
1 parent 3d04c35 commit 762f0a9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions leetcode_problemset/problemsets/subsets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> getAllSubsets(vector<int> &arr, vector<int> &out, int i, int n) {

vector<vector<int>> ans;

if (i >= n) {
ans.emplace_back(out);
return ans;
}

// not pick
vector<vector<int>> not_pick = getAllSubsets(arr, out, i + 1, n);
ans.insert(ans.end(), not_pick.begin(), not_pick.end());

// pick
out.emplace_back(arr[i]);
vector<vector<int>> pick = getAllSubsets(arr, out, i + 1, n);
ans.insert(ans.end(), pick.begin(), pick.end());
out.pop_back();

return ans;
}

vector<vector<int>> subsets(vector<int>& nums) {
vector<int> out;
vector<vector<int>> res = getAllSubsets(nums, out, 0, nums.size());
return res;
}
30 changes: 30 additions & 0 deletions leetcode_problemset/problemsets/sum_of_all_subset_xor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

int getAllSubsets(vector<int> &arr, vector<int> &ans, int i, int n) {

if(i >= n) {
int xor_cal = 0;
for(int k = 0; k < (int)ans.size(); ++k) {
xor_cal ^= ans[k];
}
return xor_cal;
}

// not pick
int not_pick = getAllSubsets(arr,ans,i+1,n);

// pick
ans.emplace_back(arr[i]);
int pick = getAllSubsets(arr,ans,i+1,n);
ans.pop_back();

return not_pick + pick;
}

int subsetXORSum(vector<int> &nums){

vector<int> ans;
return getAllSubsets(nums,ans,0,nums.size());

}

0 comments on commit 762f0a9

Please sign in to comment.