利用java怎么對(duì)集合的子集進(jìn)行求解

利用java怎么對(duì)集合的子集進(jìn)行求解?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

成都創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元慶云做網(wǎng)站,已為上家服務(wù),為慶云各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

 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; 
} 



關(guān)于利用java怎么對(duì)集合的子集進(jìn)行求解問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前標(biāo)題:利用java怎么對(duì)集合的子集進(jìn)行求解
URL網(wǎng)址:http://muchs.cn/article46/pipehg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)App設(shè)計(jì)網(wǎng)站改版、手機(jī)網(wǎng)站建設(shè)、虛擬主機(jī)面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化