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?
LOCVPS launched the Double 11 promotion on the 19...
The company's wireless WiFi signal is strong,...
SDN (Software Defined Networking) has become one ...
Preface As car travel becomes increasingly intell...
【51CTO.com original article】 Table of contents 1....
A new report from Future Market Insights (FMI) de...
The Internet we are familiar with used to mainly ...
[[435879]] The China CIO Alliance (CCA) was held ...
V5.NET has announced the news of new cloud server...
InterContinental Hotel Shenzhen OCT, Shenzhen Tel...
How far has 5G construction progressed? [[424068]...
No one likes audits. As an IT manager, the thing ...
[51CTO.com original article] The most tense and e...
Usually when we open a web page, such as a shoppi...
5G technology has the characteristics and advanta...