zoukankan      html  css  js  c++  java
  • 【基础算法】- 打印所有子集合

    import java.util.ArrayList;
    import java.util.List;
    
    
    public class SubSetCount {
    
     public static void main(String[] args) {
      
      String test = new String("ABCD");
      char[] a = test.toCharArray();
      double size = Math.pow(a.length, 2);
      List<String> result = new ArrayList<String>();
      int index = (1 << 1) - 1;
      final char[] array = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
      for(int i = 0 ; i < size ; i++){
       if(i == 0) {result.add("{}");continue;}
       int value = i;
       char[] temp = new char[32];
       int t = temp.length;
       do{
        temp[--t] = array[ value & index];
        value >>>= 1;
       }while(value != 0);
       StringBuffer at = new StringBuffer(new String(temp,t,temp.length - t));
       if(at.length() < test.length()){
        int l = at.length();
        for(int j = 0 ; j < test.length() - l ; j++){
         at.insert(0, '0');
        }
       }
       String r = "";
       boolean sign = false;
       for(int k = 0 ; k < at.length() ; k++){
        if(at.charAt(k) == '1'){
         r += a[k];
         sign = true;
        }
       }
       if(sign)
       result.add(r.intern().trim());
      }
      for(int i = 0 ; i < result.size() ; i++){
       System.out.println(result.get(i));
      }
     }
    }
    
  • 相关阅读:
    我回来了
    wget 官方jdk
    linux rpm命令安装卸载 初步使用
    关于一些对location认识的误区(转)
    直接插入排序
    冒泡排序
    Wireshark下TCP三次握手四次挥手
    linux内存使用率详解
    Linux下硬盘使用率详解及shell脚本实现
    Linux下CPU使用率详解
  • 原文地址:https://www.cnblogs.com/lixusign/p/3753410.html
Copyright © 2011-2022 走看看