原创转载请注明出处:http://agilestyle.iteye.com/blog/2360659
找出一个Set集合中的所有子集
比如全集为 {1, 2, 3}
子集则有{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}
此算法的核心思想:递归
package org.fool.java.collections; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class GetAllSubSetTest { public static void main(String[] args) { Set<Integer> set = new HashSet<>(); set.add(1); set.add(2); set.add(3); set.add(4); // set to array Integer[] nums = set.toArray(new Integer[set.size()]); // use lambda to transform Integer[] to int[] printSubSet(Arrays.stream(nums).mapToInt(i -> i).toArray()); } // step 1: decide how many elements in a sub-set to be printed public static void printSubSet(int[] nums) { for (int i = 0; i <= nums.length; i++) { boolean[] ifPrint = new boolean[nums.length]; printSubSet(nums, ifPrint, 0, i); // start from 0th index, and the size varies for the loop } } // step 2: Recursively process elements from left to right to print out all possible combination of elements with fixed size // we need three additional variables to keep track of status // boolean array to know whether printed out or not // start is the start index to be printed to prevent duplicates // remain is keeping track of how many remaining elements to be processed for the subset action public static void printSubSet(int[] nums, boolean[] ifPrint, int start, int remain) { // firstly if remain == 0, we done! if (remain == 0) { System.out.print("{"); // check each ifPrint status to decide print or not for (int i = 0; i < ifPrint.length; i++) { if (ifPrint[i]) { System.out.print(nums[i] + ", "); } } System.out.print("}\n"); } else { // we need process char by char from the start position until end // before that, we need determine whether we proceed or not to check if (start + remain > nums.length) if (start + remain > nums.length) { } else { for (int i = start; i < nums.length; i++) { // now before we come to recursive part we have to make sure this position is not used if (!ifPrint[i]) { // now assign its value to true as used indicator ifPrint[i] = true; // recursive call printSubSet(nums, ifPrint, i + 1, remain - 1); // set the position back to false and proceed from next element ifPrint[i] = false; } } } } } }
Console Output
Reference
https://www.youtube.com/watch?v=wVNV3JEcikA
相关推荐
You can just combine the intensities from all the color channels for the pixels into one long vector, as if you were working with a grayscale image with 3x the number of pixels as the original image.
### ASN.1、BER与DER基础知识详解 #### 引言 随着软件开发复杂度的不断提高,抽象成为管理软件设计过程中的关键原则之一。通过抽象,设计师可以在不关心具体实现细节的情况下定义系统的一部分,从而简化了整体的...
It provides detailed information about the instructions from A to M, which is a subset of the entire instruction set available for these architectures. #### Detailed Explanation of Key Knowledge ...
is-subset-of验证数组还是对象是子集。 状态 类别 状态 版本 依存关系 开发依赖 建造 执照 安装 $ npm install is-subset-of 快速开始 首先,您需要在应用程序中添加对is-subset-of的引用: const { isSubsetOf...
It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image. It is a good database for people who want to try learning techniques and ...
Use an approximate algorithm to find the sum of subsets. S is a set of positive integers. Find a subset of S whose sum should be as large as possible but cannot exceed t
关于 $\omega$ 的一个奇特子集的研究,赵峰,,根据 $\mbox{\upshape{ZF}}$ 公理,存在极限序数 $\omega$ 的一个奇特子集 $\bar{\omega}$,它既是无穷的,又是D\,–有穷的。只要 $\mbox{\upshape{ZF}}$ 公理�
It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image. It is a good database for people who want to try learning techniques and ...
Netflix also identified a probe subset of 1,408,395 ratings within the training data set. The probe, quiz, and test data sets were chosen to have similar statistical properties. In summary, the data...
对类P是类NP的真子集猜测的第二证明,徐万东,,判定一个任意的无向可平面图G是否是哈密顿图问题是六个重要的NP完全问题之一(HC)。本文根据判定除马图以外的任意无向图是否是哈密��
The degree of a table is the number of _____ in the table. (a) keys (b) columns (c) rows (d) foreign keys Correct answer is (b) Your score on this question is: 10.00 Feedback: (b) ...
EPI includes a study guide with several scenarios, ranging from weekend Hackathon to semester long preparation with a recommended a subset of problems for each scenario. All problems are classified...
"complement of B with respect to A"是A相对于B的补集,即B中不在A内的所有元素的集合。"symmetric difference"是两个集合中互不相同元素的集合。 "commutative"、"associative"和"distributive"描述了运算的性质...
EPI includes a study guide with several scenarios, ranging from weekend Hackathon to semester long preparation with a recommended a subset of problems for each scenario. All problems are classified...
It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image. It is a good database for people who want to try learning techniques and ...
A substantial subset of the web data follows some kind of underlying structure. Nevertheless, HTML does not contain any schema or semantic information about the data it represents. A program able to ...
Informatica Data Subset是一款高效的数据管理工具,专门设计用于解决企业在非生产环境中面临的挑战。随着数据量的不断增长,IT组织需要处理的不仅是生产系统,还有大量用于测试、开发和培训的数据副本。传统的做法...
根据提供的文件信息,我们可以总结出以下关于“Subset-098 v100 .pdf”文档中的关键知识点,主要围绕“Subset 098 RBC-RBC安全通信协议”的相关内容展开。 ### 标题:Subset-098 v100 .pdf 此标题表明文档的版本为...
aimed at adaptively ascertaining the pixel level data associations between a reference image and all the elements of a source image set. Namely, we address the question, what aggregation subset of the...