MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / backtrack

Function backtrack

cpp/Backtracking/find_all_subsets.cpp:10–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8}
9
10void backtrack(int i, std::vector<int>& currSubset, std::vector<int>& nums, std::vector<std::vector<int>>& res) {
11 // Base case: if all elements have been considered,
12 // add the current subset to the output.
13 if (i == nums.size()) {
14 res.push_back(currSubset);
15 return;
16 }
17 // Include the current element and recursively explore all paths
18 // that branch from this subset.
19 currSubset.push_back(nums[i]);
20 backtrack(i + 1, currSubset, nums, res);
21 // Exclude the current element and recursively explore all paths
22 // that branch from this subset.
23 currSubset.pop_back();
24 backtrack(i + 1, currSubset, nums, res);
25}

Callers 7

findAllSubsetsFunction · 0.70
backtrackMethod · 0.50
FindAllPermutationsMethod · 0.50
backtrackMethod · 0.50
FindAllSubsetsMethod · 0.50
backtrackMethod · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected