let 78 Subsets

本文介绍了一种生成给定整数数组所有可能子集(幂集)的算法,并提供了详细的AC代码实现。该算法采用回溯法,确保解决方案集中不包含重复的子集。
Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

主题思想: 这个和77 题同样是通过回溯完成的,形成了一个比较典型的方法吧

AC 代码:

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        dfs(nums,ans,new ArrayList<Integer>(),0);
        return ans;
    }
    public void dfs(int[] nums,List<List<Integer>>ans,List<Integer> tmp,int start){

        ans.add(new ArrayList<Integer>(tmp));
        for(int i=start;i<nums.length;i++){
            tmp.add(nums[i]);
            dfs(nums,ans,tmp,i+1);
            tmp.remove(tmp.size()-1);
        }
    }
}
(i) To show that X △ Y = (X ∪ Y ) − (X ∩ Y ), we need to show that every element in X △ Y is in (X ∪ Y ) − (X ∩ Y ) and vice versa. Let x be an element in X △ Y. Then, x is either in X - Y or Y - X. If x is in X - Y, then x is in X ∪ Y and not in X ∩ Y. Therefore, x is in (X ∪ Y ) − (X ∩ Y ). Similarly, if x is in Y - X, then x is in X ∪ Y and not in X ∩ Y, so x is in (X ∪ Y ) − (X ∩ Y ). Therefore, every element in X △ Y is in (X ∪ Y ) − (X ∩ Y ). Now, let x be an element in (X ∪ Y ) − (X ∩ Y ). Then, x is in either X or Y but not both. Without loss of generality, assume x is in X. Then, x is not in X ∩ Y, so x is not in Y. Therefore, x is in X - Y, which means that x is in X △ Y. Similarly, if x is in Y but not X, then x is in Y - X and hence in X △ Y. Therefore, every element in (X ∪ Y ) − (X ∩ Y ) is in X △ Y. Hence, we have shown that X △ Y = (X ∪ Y ) − (X ∩ Y ). (ii) To show that (M − X) △ (M − Y ) = X △ Y, we need to show that every element in (M − X) △ (M − Y ) is in X △ Y and vice versa. Let x be an element in (M − X) △ (M − Y ). Then, x is either in (M − X) - (M − Y ) or in (M − Y ) - (M − X). If x is in (M − X) - (M − Y ), then x is in Y but not X. Therefore, x is in X △ Y. Similarly, if x is in (M − Y ) - (M − X), then x is in X but not Y, and hence x is in X △ Y. Therefore, every element in (M − X) △ (M − Y ) is in X △ Y. Now, let x be an element in X △ Y. Then, x is either in X - Y or Y - X. If x is in X - Y, then x is in (M − Y ) - (M − X), so x is in (M − X) △ (M − Y ). Similarly, if x is in Y - X, then x is in (M − X) - (M − Y ), and hence x is in (M − X) △ (M − Y ). Therefore, every element in X △ Y is in (M − X) △ (M − Y ). Hence, we have shown that (M − X) △ (M − Y ) = X △ Y. (iii) To show that the symmetric difference is associative, i.e., (X △ Y ) △ Z = X △ (Y △ Z), we need to show that every element in (X △ Y ) △ Z is in X △ (Y △ Z) and vice versa. Let x be an element in (X △ Y ) △ Z. Then, x is either in (X - Y) - Z or in (Y - X) - Z or in Z - (X △ Y ). If x is in (X - Y) - Z, then x is in X △ (Y △ Z) since x is in X but not in Y △ Z. Similarly, if x is in (Y - X) - Z, then x is in X △ (Y △ Z) since x is in Y but not in X or Z. Finally, if x is in Z - (X △ Y ), then x is either in Z - X and not in Y or in Z - Y and not in X. In the former case, x is in X △ (Y △ Z) since x is in X but not in Y △ Z. In the latter case, x is in X △ (Y △ Z) since x is in Y but not in X or Z. Therefore, every element in (X △ Y ) △ Z is in X △ (Y △ Z). Now, let x be an element in X △ (Y △ Z). Then, x is either in X - (Y △ Z) or in (Y △ Z) - X. If x is in X - (Y △ Z), then x is either in X - Y or in X - Z. Without loss of generality, assume x is in X - Y. Then, x is in (X - Y) - Z and hence in (X △ Y ) △ Z. Similarly, if x is in (Y △ Z) - X, then x is either in Y - X or in Z - X. Without loss of generality, assume x is in Y - X. Then, x is in (Y - X) - Z and hence in (X △ Y ) △ Z. Therefore, every element in X △ (Y △ Z) is in (X △ Y ) △ Z. Hence, we have shown that the symmetric difference is associative. (iv) To show that X ∩ (Y △ Z) = (X ∩ Y ) △ (X ∩ Z), we need to show that every element in X ∩ (Y △ Z) is in (X ∩ Y ) △ (X ∩ Z) and vice versa. Let x be an element in X ∩ (Y △ Z). Then, x is in X and x is in Y or x is in Z but not both. Without loss of generality, assume x is in Y but not Z. Then, x is in X ∩ Y but not in X ∩ Z, so x is in (X ∩ Y ) △ (X ∩ Z). Therefore, every element in X ∩ (Y △ Z) is in (X ∩ Y ) △ (X ∩ Z). Now, let x be an element in (X ∩ Y ) △ (X ∩ Z). Then, x is either in (X ∩ Y) - (X ∩ Z) or in (X ∩ Z) - (X ∩ Y). Without loss of generality, assume x is in (X ∩ Y) - (X ∩ Z). Then, x is in X and x is in Y but not in Z, so x is in X ∩ (Y △ Z). Therefore, every element in (X ∩ Y ) △ (X ∩ Z) is in X ∩ (Y △ Z). Hence, we have shown that X ∩ (Y △ Z) = (X ∩ Y ) △ (X ∩ Z). (v) To show that X △ Y = Z △ W iff X △ Z = Y △ W, we need to show that if X △ Y = Z △ W, then X △ Z = Y △ W and vice versa. Assume that X △ Y = Z △ W. Then, (X - Y) ∪ (Y - X) = (Z - W) ∪ (W - Z) (X - Y) ∪ (Y - X) = (Z ∩ W') ∪ (W ∩ Z') (X - Y) ∪ (Y - X) = (Z ∪ W) - (Z ∩ W) - (W ∩ Z) + (Z ∩ W) (X ∪ Z') ∩ (Y ∪ W') ∪ (X' ∪ Z) ∩ (Y' ∪ W) = (X ∪ W') ∩ (Y ∪ Z') ∪ (X' ∪ W) ∩ (Y' ∪ Z) Now, let's simplify the left-hand side of this equation. We have: (X ∪ Z') ∩ (Y ∪ W') ∪ (X' ∪ Z) ∩ (Y' ∪ W) = [(X ∪ Z') ∩ (X' ∪ Z)] ∪ [(X ∪ Z') ∩ (Y' ∪ W)] ∪ [(Y ∪ W') ∩ (X' ∪ Z)] ∪ [(Y ∪ W') ∩ (Y' ∪ W)] = [(X ∩ Z') ∪ (X' ∩ Z)] ∪ [(X ∩ Y') ∪ (Z' ∩ W)] ∪ [(Y ∩ X') ∪ (W' ∩ Z)] ∪ [(Y ∩ W') ∪ (Y' ∩ W)] Similarly, we can simplify the right-hand side of the equation: (X ∪ W') ∩ (Y ∪ Z') ∪ (X' ∪ W) ∩ (Y' ∪ Z) = [(X ∩ W') ∪ (X' ∩ W)] ∪ [(X ∩ Z') ∪ (Y' ∩ Z)] ∪ [(Y ∩ W') ∪ (X' ∩ Y)] ∪ [(Y ∩ Z') ∪ (Y' ∩ Z)] Now, we can see that the equation holds if and only if every term on the left-hand side appears exactly once on the right-hand side. For example, the term (X ∩ Z') appears on the left-hand side but not on the right-hand side, so it must cancel out with another term that appears on the right-hand side but not on the left-hand side. By inspecting the two simplified expressions, we can see that this is indeed the case. Therefore, X △ Z = Y △ W. Conversely, assume that X △ Z = Y △ W. Then, using a similar argument as above, we can show that X △ Y = Z △ W. Hence, we have shown that X △ Y = Z △ W iff X △ Z = Y △ W. (vi) The region of X △ Y △ Z in a Venn diagram is the region that is shaded in exactly one of the three circles X, Y, Z. (vii) A sketch of a Venn diagram for 4 distinct sets is shown below: ``` A B o-----o-----o |\ | /| | \ | / | C o--\--o--/--o D | \ | / | | \|/ | o-----o-----o E F ``` In this diagram, the regions A, B, C, D, E, F, AB, AC, AD, BC, BD, CD, ABC, ABD, ACD, BCD, and ABCD represent the different subsets of the four sets. The region that is shaded in exactly one of the four circles represents the symmetric difference of those sets.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值