【基础算法】- 打印所有子集合

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));
  }
 }
}
原文地址:https://www.cnblogs.com/lixusign/p/3753410.html