java求解集合的子集的實(shí)例
java求解集合的子集的實(shí)例
方式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
利用二進(jìn)制的對應(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)文章
利用java監(jiān)聽器實(shí)現(xiàn)在線人數(shù)統(tǒng)計
過去使用ASP和ASP.NET兩種編程的時候,都寫過在線人數(shù)統(tǒng)計能,實(shí)現(xiàn)功能挺簡單的!今天使用java來實(shí)現(xiàn)在線人數(shù)統(tǒng)計有點(diǎn)另類,是通過Java監(jiān)聽器實(shí)現(xiàn)的,需要的朋友可以參考下2015-09-09
Eclipse如何導(dǎo)入Maven項目詳解(新手初學(xué))
這篇文章主要介紹了Eclipse如何導(dǎo)入Maven項目詳解(新手初學(xué)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Java Mybatis中的 ${ } 和 #{ }的區(qū)別使用詳解
這篇文章主要介紹了Mybatis中的 ${ } 和 #{ }的區(qū)別使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
微信游戲打飛機(jī)游戲制作(java模擬微信打飛機(jī)游戲)
java模擬微信打飛機(jī)游戲,大家參考使用吧2013-12-12
springboot 在ftl頁面上使用shiro標(biāo)簽的實(shí)例代碼
這篇文章主要介紹了springboot 在ftl頁面上使用shiro標(biāo)簽的實(shí)例代碼,通過文字說明結(jié)合實(shí)例的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-05-05

