Home > Java > javaTutorial > Java example code sharing for solving set subsets

Java example code sharing for solving set subsets

黄舟
Release: 2017-10-16 10:07:48
Original
1577 people have browsed it

This article mainly introduces relevant information about examples of solving subsets of sets in Java. I hope this article can help everyone master this method. Friends in need can refer to it

Java example of solving a subset of a set

Method 1: We know the nth power of the number of subsets 2

For example, the subsets of 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

Using binary correspondence


@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(&#39;b&#39;,&#39;c&#39;,&#39;d&#39;)); 
  System.out.println(subsets); 
  System.out.println(subsets2); 
  System.out.println(subsets3); 
} 
 
//集合接受各种类型数据 
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; 
}
Copy after login

Method 2: Induction method


   @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; 
}
Copy after login

The above is the detailed content of Java example code sharing for solving set subsets. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template