java求解集合的子集的實(shí)例
java求解集合的子集的實(shí)例
方式1:我們知道子集個(gè)數(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
利用二進(jìn)制的對(duì)應(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 歸納法 //從{}和最后一個(gè)元素開始,每次迭代加一個(gè)元素組成一個(gè)新的集合 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; }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- java使用回溯法求解數(shù)獨(dú)示例
- java求解漢諾塔問(wèn)題示例
- Java采用循環(huán)鏈表結(jié)構(gòu)求解約瑟夫問(wèn)題
- java 求解二維數(shù)組列最小值
- Java編程用棧來(lái)求解漢諾塔問(wèn)題的代碼實(shí)例(非遞歸)
- Java背包問(wèn)題求解實(shí)例代碼
- java編程實(shí)現(xiàn)求解八枚銀幣代碼分享
- Java語(yǔ)言求解完美數(shù)代碼分析
- java語(yǔ)言求解兔子問(wèn)題代碼分析
- java模仿windows計(jì)算器示例
- Java實(shí)現(xiàn)求解一元n次多項(xiàng)式的方法示例
相關(guān)文章
子類繼承父類時(shí)構(gòu)造函數(shù)相關(guān)問(wèn)題解析
這篇文章主要介紹了子類繼承父類時(shí)構(gòu)造函數(shù)相關(guān)問(wèn)題解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Spring 中@Validated 分組校驗(yàn)的使用解析
這篇文章主要介紹了Spring 中@Validated 分組校驗(yàn)的使用解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10詳解自動(dòng)注冊(cè)Gateway網(wǎng)關(guān)路由配置
這篇文章主要為大家介紹了自動(dòng)注冊(cè)Gateway網(wǎng)關(guān)路由配置的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03SpringBoot如何使用TestEntityManager進(jìn)行JPA集成測(cè)試
TestEntityManager是Spring Framework提供的一個(gè)測(cè)試框架,它可以幫助我們進(jìn)行 JPA 集成測(cè)試,在本文中,我們將介紹如何使用 TestEntityManager 進(jìn)行 JPA 集成測(cè)試,感興趣的跟著小編一起來(lái)學(xué)習(xí)吧2023-06-06詳解springboot?springsecuroty中的注銷和權(quán)限控制問(wèn)題
這篇文章主要介紹了springboot-springsecuroty?注銷和權(quán)限控制,賬戶注銷需要在SecurityConfig中加入開啟注銷功能的代碼,權(quán)限控制要導(dǎo)入springsecurity和thymeleaf的整合依賴,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-03-03SpringBoot+WebSocket+Netty實(shí)現(xiàn)消息推送的示例代碼
這篇文章主要介紹了SpringBoot+WebSocket+Netty實(shí)現(xiàn)消息推送的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04SpringBoot文件上傳大小設(shè)置方式(yml中配置)
這篇文章主要介紹了SpringBoot文件上傳大小設(shè)置方式(yml中配置),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03