`
liyonghui160com
  • 浏览: 761377 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Apriori算法求数组的非空子集java代码

阅读更多

 

Apriori算法求集合的非空子集java代码

 

 

public class Test {
        public static void main(String[] args) {
            String str="abcd" ;
            //用Set集合保存结保证内容重复
            Set<String> set = new HashSet<String>();
            //外层循环控制指针移动位置 从 a 依次移动顺序   a b  c a c
            for(int i=0 ; i<str.length() ; i++){
                //从指针所位置开始内层遍历
                for(int k= i ; k<str.length() ; k++){
                    //从指针位置处开始截取字符串直截 字符串结尾
                    String res = str.substring(i , k+1);
                    //当前字符串和原始字符串相同放入集合该种情况子串
                    if(res ==str) continue ;
                    set.add(res);
                }
            }
            //遍历结
            for(String s : set){
                System.out.println(s);
            }
        }
}

 

 

 

 

public class Test {

    /**
     * 打印一个数组所有的非空子集
     */
    public List<String> printAllSubsets(String[] array) {
        if (null == array || 0 == array.length) {
            throw new IllegalArgumentException("数组不能为Null,至少有一个元素");
        }
        int len = array.length;
        List<String> stringList = new LinkedList<String>();
        int allMasks = 1 << len;
        // 遍历所有的二进制表示方式
        for (int i = 1; i < allMasks; i++) {
            if (i == allMasks - 1) break;
            StringBuilder s = new StringBuilder();
            for (int j = 0; j < len; j++)
                if ((i & (1 << j)) > 0) {
                    s.append(array[j]);
                }
            stringList.add(s.toString());
        }
        return stringList;
    }

    public static void main(String[] args) {
        Test exam = new Test();
        List<String> stringList = exam.printAllSubsets(new String[]{"aa", "bb", "ce", "dh"});
        for (String s : stringList) {
            System.out.println(s);
        }
    }
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics