LeeCode (bit, dfs) 78_subset

LeeCode (bit, dfs) ​​78_subset

Problem:
Given a set of integer arrays nums without repeated elements, return all possible subsets (power sets) of the array.

Note: The solution set cannot contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2 ],
[]
]

Source: LeetCode
Link: https://leetcode-cn.com/problems/subsets
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Problem-solving ideas:

Method 1:
Iteratively implement subset enumeration

  1. The length of the original sequence is n, and the state ai of the i-th number has two cases:
    1. In the subset: use 1 to represent
    2. Not in the subset: use 0 to represent
  2. For example, when n=3 and a={5,2,9}:

Insert picture description here

  1. It can be found that the binary number corresponding to the 0/1 sequence is exactly from 0 to 2^n-1
  2. We can enumerate that the binary representation of mask∈[0,2^n−1] is a sequence of 0/1, and we can take numbers from the original set according to this sequence of 0/1. When we have enumerated all 2^n masks, we can also construct all the subsets.

Java code:

import java.util.ArrayList;
import java.util.List;

public class 子集 {
    
    
	List<Integer> t = new ArrayList<Integer>();
	List<List<Integer>> ans = new ArrayList<List<Integer>>();
	
	 public List<List<Integer>> subsets(int[] nums) {
    
    
		int n = nums.length;
		for(int mask=0;mask<(1<<n);mask++){
    
    
			t.clear();
			for(int i=0;i<n;i++){
    
    
				if((mask & (1<<i))!=0){
    
    
					t.add(nums[i]);
				}
			}
			ans.add(new ArrayList<Integer>(t));
		}
		return ans;
	 }
}

It should be noted that:
ans.add(new ArrayList(t)); This line of code cannot be written as ans.add(t);
because if you use ans.add(t); to add data, the data added in ans is shallow copy data ,
Will be changed due to subsequent changes in t, a new instance should be used to create a new memory space for deep copying of the data of ans.

Method 2:
Recursive dfs implements subset enumeration

  1. The variable cur is the current position, and when the boundary is cur equal to the length of nums, the temporary subset t is added to the result set ans.
  2. dfs considers two cases:
    1.nums[cur] is in the subset
    2.nums[cur] is not in the subset

Java code:

import java.util.ArrayList;
import java.util.List;

public class 子集 {
    
    
	List<Integer> t = new ArrayList<Integer>();
	List<List<Integer>> ans = new ArrayList<List<Integer>>();
	
	public List<List<Integer>> subsets(int[] nums) {
    
    
		dfs(0,nums);
		return ans;
	 }
	
	public void dfs(int cur,int[] nums){
    
    
		//边界
		if(cur==nums.length){
    
    
			ans.add(new ArrayList<Integer>(t));
			return;
		}
		
		//两种dfs情况(nums[cur]放入子集 和 nums[cur]不放入子集)
		t.add(nums[cur]);
		dfs(cur+1,nums);
		//回溯
		t.remove(t.size()-1);
		dfs(cur+1,nums);
	}	 
}

Guess you like

Origin blog.csdn.net/u013456390/article/details/111930974