After understanding the essence, this is a template question SubsetLink to LeetCode questions: https://leetcode-cn.com/problems/subsets/ Given an integer array nums with no repeated elements, return all possible subsets (power sets) of the array. Note: The solution set cannot contain repeated subsets. Example: Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] IdeasThe subset problem is different from 77. Combination and 131. Splitting palindrome strings. If we abstract the subset problem, combination problem, and partition problem into a tree, then the combination problem and partition problem are to collect the leaf nodes of the tree, while the subset problem is to find all the nodes of the tree! In fact, subset is also a combinatorial problem, because its set is unordered, the subset {1,2} and the subset {2,1} are the same. Since it is unordered, the elements that have been taken will not be taken repeatedly. When writing the backtracking algorithm, for should start from startIndex instead of starting from 0! Some students asked, when can for start from 0? When solving permutation problems, we have to start from 0, because sets are ordered. {1, 2} and {2, 1} are two sets. We will talk about permutation problems in subsequent articles. Taking nums = [1,2,3] in the example, we can abstract the subset into a tree structure as follows: Subset From the red line part in the figure, we can see that when traversing the tree, all the nodes are recorded, which is the required subset set. Back to the trilogy
The global variable array path collects elements for the subset, and the two-dimensional array result stores the subset combinations. (It can also be put into the recursive function parameters) The recursive function parameters are mentioned above, and startIndex is required. The code is as follows:
Recursion termination condition As can be seen from the figure: Subset When the remaining set is empty, it is a leaf node. So when is the remaining set empty? That is, startIndex is greater than the length of the array, so it terminates because there are no elements to be taken. The code is as follows:
In fact, there is no need to add a termination condition, because startIndex >= nums.size(), and the for loop at this level has already ended.
To find the subset problem, no pruning is required! Because the subset is to traverse the entire tree. Then the single-layer recursive logic code is as follows:
C++ CodeAccording to the backtracking algorithm, you should know this! The backtracking algorithm template given is:
You can write the following backtracking algorithm C++ code:
In the comments, you can see that you don't need to write the termination condition because we are going to traverse the entire tree. Some students may worry about whether there will be infinite recursion if the termination condition is not written. No, because the next layer of each recursion starts from i+1. This article is reprinted from the WeChat public account "Code Random Thoughts", which can be followed through the following QR code. To reprint this article, please contact the Code Random Thoughts public account. |
<<: Promote 5G integration and innovation and accelerate the digital transformation of the industry
>>: When the 2G/3G network is down, will your IoT work properly?
If there is one common demand among governments a...
[[180647]] The bell of 2017 has rung. Facing the ...
Enterprises that need to upgrade their traditiona...
On July 27, Huawei Cloud held the TechWave AI Day...
According to a report by Japan's Jiji Press o...
This question has a tricky part. It asks you to d...
Hey everyone, this is cxuan. Today we are going t...
[[390044]] This article is reprinted from the WeC...
FirstByte is a regular Russian hosting company fo...
My home was recently renovated. As a computer pro...
GPP defines network slicing as one of the main fu...
spinservers has a large stock of 1Gbps unlimited ...
According to data disclosed by the Ministry of In...
On July 11, Wangsu Technology announced the launc...
HostXen is a domestic hosting merchant founded in...