java求解集合的子集的實例
java求解集合的子集的實例
方式1:我們知道子集個數(shù) 2的n次方
比如a,b,c的子集
* 000 0 {}
*001 1 a
*010 2 b
*011 3 a,b (b,a)
*100 4 c
* 101 5 a,c (c,a)
* 110 6 b,c (c,b)
* 111 7 a,b,c
利用二進制的對應(yīng)關(guān)系
@Test
public void test1() throws Exception {
Set<ArrayList<Integer>> subsets = getSubsets( Arrays.asList(1,2,6));
Set<ArrayList<String>> subsets2 = getSubsets( Arrays.asList("a","b","c"));
Set<ArrayList<Character>> subsets3 = getSubsets( Arrays.asList('b','c','d'));
System.out.println(subsets);
System.out.println(subsets2);
System.out.println(subsets3);
}
//集合接受各種類型數(shù)據(jù)
public <T> Set<ArrayList<T>> getSubsets(List<T> subList) {
//考慮去重
Set<ArrayList<T>> allsubsets = new LinkedHashSet<>();
int max = 1 << subList.size();
for (int loop = 0; loop < max; loop++) {
int index = 0;
int temp = loop;
ArrayList <T> currentCharList = new ArrayList<T>();
//控制索引
while (temp > 0) {
if ((temp & 1) > 0) {
currentCharList.add(subList.get(index));
}
temp >>= 1;
index++;
}
allsubsets.add(currentCharList);
}
return allsubsets;
}
方式2:歸納法
@Test
public void testName() throws Exception {
Set<List<Integer>> subsets2 = getSubsets2(Arrays.asList(1,2,3));
System.out.println(subsets2);
}
//方式2 歸納法
//從{}和最后一個元素開始,每次迭代加一個元素組成一個新的集合
public Set<List<Integer>> getSubsets2(List<Integer> list) {
if (list.isEmpty()) {
Set<List<Integer>> ans=new LinkedHashSet<>();
ans.add(Collections.emptyList());
return ans;
}
Integer first=list.get(0);
List<Integer> rest=list.subList(1, list.size());
Set<List<Integer>> list1 = getSubsets2(rest);
Set<List<Integer>> list2 = insertAll(first, list1);//
System.out.println(list1);
System.out.println(list2);
System.out.println("================");
return concat(list1, list2);
}
public Set<List<Integer>> insertAll(Integer first,Set<List<Integer>> lists){
//
Set<List<Integer>> result=new LinkedHashSet<>();
for (List<Integer> list : lists) {
List<Integer> copy=new ArrayList<>();
copy.add(first);
copy.addAll(list);
result.add(copy);
}
return result;
}
//這樣寫可以不影響lists1,lists2的值
private Set<List<Integer>> concat(Set<List<Integer>> lists1,Set<List<Integer>> lists2) {
Set<List<Integer>> temp=new LinkedHashSet<>(lists1);
temp.addAll(lists2);
return temp;
}
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
子類繼承父類時構(gòu)造函數(shù)相關(guān)問題解析
這篇文章主要介紹了子類繼承父類時構(gòu)造函數(shù)相關(guān)問題解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
詳解自動注冊Gateway網(wǎng)關(guān)路由配置
這篇文章主要為大家介紹了自動注冊Gateway網(wǎng)關(guān)路由配置的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
SpringBoot如何使用TestEntityManager進行JPA集成測試
TestEntityManager是Spring Framework提供的一個測試框架,它可以幫助我們進行 JPA 集成測試,在本文中,我們將介紹如何使用 TestEntityManager 進行 JPA 集成測試,感興趣的跟著小編一起來學(xué)習(xí)吧2023-06-06
詳解springboot?springsecuroty中的注銷和權(quán)限控制問題
這篇文章主要介紹了springboot-springsecuroty?注銷和權(quán)限控制,賬戶注銷需要在SecurityConfig中加入開啟注銷功能的代碼,權(quán)限控制要導(dǎo)入springsecurity和thymeleaf的整合依賴,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2022-03-03
SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼
這篇文章主要介紹了SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
SpringBoot文件上傳大小設(shè)置方式(yml中配置)
這篇文章主要介紹了SpringBoot文件上傳大小設(shè)置方式(yml中配置),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

