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?
[[267883]] 1. Seven-layer network model The http ...
[[435239]] In Japan, a trial project to wirelessl...
Communication technology, as a basic technology, ...
October 11, 2023 – Akamai Technologies, Inc. (NAS...
The day before yesterday, we shared the product i...
Why is Software Lifecycle Management Critical to ...
Get detailed information about your network conne...
The continuous emergence and upgrading of online ...
This morning, the National Development and Reform...
1. Introduction The Internet is full of eavesdrop...
Network connection and communication are the know...
WePC uses the .au domain name and describes itsel...
Airtable, an overseas multi-dimensional spreadshe...
Internet of Vehicles Security Requires a Platform...
Miao Wei, deputy director of the Economic Committ...