求一个集合的全部子集

 常见面试题:求一个集合的全部子集

  问题实例化:求一个集合{1,2,3}的全部子集?

  第一步:空集{} 是一个非空集合的子集 

  第二步:从集合的最右边开始,3 与{} 组合成新的元素集合{3},{}

  第三步:从集合的最右边往左移一个,元素2 和 新的集合群{3},{} 组合成新的集合群 {2,3},{2},{3},{}

  第四步:{1,2,3},{1,2},{1,3},{1},{2,3},{2},{3},{}

 

  集合 {1,2,3,4,5......n} 有哪些特新呢?

  子集个数:2n 

  每个元素出现的次数 

 

  以下代码用了递归、for循环和二进制映射方法实现

   

import java.util.ArrayList;
import java.util.List;

/** 
* @author : cai**
* @date 创建时间:2016年12月24日 上午10:41:46 
* @version 1.0 
* @parameter  
* @since  
* @return  
*/
public class TestCollection {
    

    /**
     *  @function find all subsets under a set
     *     @method: use  recurrence
     *  @param list
     *  @param index
     *  @return ArrayList<ArrayList<Object>>
     */
    public static ArrayList<ArrayList<Object>> getSubLists (ArrayList<Object> list,int index){
        //declare variables
        ArrayList<ArrayList<Object>> subLists ;
        
        if(index == list.size()){
            // initialize variable
            subLists = new ArrayList<ArrayList<Object>>();
            
            //add one empty collection;
            subLists.add(new ArrayList<Object>());
        }else{
            //invoke itself method
            subLists = getSubLists(list,index+1);
            
            /**
             * achieve one element and the Collection combine
             */
            Object element = list.get(index);
            ArrayList<ArrayList<Object>> currentLists = new ArrayList<ArrayList<Object>>();
            for(ArrayList<Object> sublist : subLists){
                ArrayList<Object> tempList  = new ArrayList<Object>();
                tempList.addAll(sublist);
                tempList.add(element);
                currentLists.add(tempList);
            }
            subLists.addAll(currentLists);
        }
        return subLists;
                
    }
    
    /**
     *  @function find all subsets under a set
     *     @method: use  normal mothod
     *  @param list
     *  @return List<List<Object>>
     */
    public static List<List<Object>> getSubLists(ArrayList<Object> list){
        //get the max index
        int index = list.size()-1;
        
        //declare variables and instantiation 
        List<List<Object>> subLists = new ArrayList<List<Object>>();
        
        //get the lastElement
        subLists.add(new ArrayList<Object>());
        for(int i = index;i>=0;i--){
            List<List<Object>> tempSubList = new ArrayList<List<Object>>();
            for(List<Object> subList : subLists){
                ArrayList<Object> tempList = new ArrayList<Object>();
                tempList.add(list.get(i));
                tempList.addAll(subList);
                tempSubList.add(tempList);
            }
            subLists.addAll(tempSubList);
        }
        return subLists;
    }
    
    
    /**
     * 使用二进制bit位映射方法打印出一个集合的全部子集合
     *  @param list void
     */
    public static void printSubLists(ArrayList<Object> list){
        //get the max index
        int index ;
        int size = list.size();
        
        //declare variables and instantiation 
        List<List<Object>> subLists = new ArrayList<List<Object>>();
        
        //get the subList total number
        double  count = java.lang.Math.pow(2,size);
        
        for(long i = 0; i<count ;i++){
            index = list.size()-1;   //initialize the index 
            String l = Long.toBinaryString(i); //  convert the value of i to a binary string
            List<Object> tempList = new ArrayList<>(); //define  a temporary ArrayList
            
            /**
             * 将二进制字符串和list 从右向右一一映射
             * 比较Unicode代码 , 是否等于49(49是 数字1 的Uncode 代码)
             * 如果是则获取list相对于的元素
             */
            for(int j=l.length()-1;j>=0;j--){
                if(l.codePointAt(j)==49){
                    tempList.add(list.get(index));
                }
          index--; } System.out.println(tempList
+ "-----" + i); } } }

 

posted @ 2016-12-27 21:45  瓦肯船长  阅读(1214)  评论(0)    收藏  举报