问题描述

给定一个正整数n,生成集合 {1,2,3,…n} 的所有子集

问题思路

思路一:二进制法

利用二进制是否显现”的转换思路来解决这个问题,一个数字在子集当中就标记为1反之标记为0,就比如 n=3 ,输出: {}{1,0,0}{0,1,0}{0,0,1}{1,1,0}{1,0,1}{0,1,1}{1,1,1}

代码思路

思路一:利用动态数组数据结构

输入的n就是动态数组的初始大小
然后依次利用“吞进来”和“吐出去”尾元素来实现

java代码实现

  1. package com.wztlink1013.al._递归法_;
  2. /*
  3. * 作用:测量代码运行时间
  4. */
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. public class Times {
  8. private static final SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss.SSS");
  9. public interface Task {
  10. void execute();
  11. }
  12. public static void test(String title, Task task) {
  13. if (task == null) return;
  14. title = (title == null) ? "" : ("【" + title + "】");
  15. System.out.println(title);
  16. System.out.println("开始:" + fmt.format(new Date()));
  17. long begin = System.currentTimeMillis();
  18. task.execute();
  19. long end = System.currentTimeMillis();
  20. System.out.println("结束:" + fmt.format(new Date()));
  21. double delta = (end - begin) / 1000.0;
  22. System.out.println("耗时:" + delta + "秒");
  23. System.out.println("-------------------------------------");
  24. }
  25. }
  1. package com.wztlink1013.al._递归法_;
  2. import java.util.ArrayList;
  3. /**
  4. * 子集问题
  5. */
  6. public class SubSetting {
  7. static ArrayList<Integer> x = new ArrayList<Integer>();
  8. static int cnt = 0;
  9. public static void main(String args[]) {
  10. int n = 4;
  11. Times.test("当n = " + n + "时候的耗费时间", new Times.Task() {
  12. public void execute() {
  13. Subsetting(n);
  14. }
  15. });
  16. }
  17. private static void Subsetting(int n) {
  18. if (n > 0) {
  19. x.add(0);
  20. Subsetting(n - 1);
  21. x.remove(x.size() - 1);
  22. x.add(1);
  23. Subsetting(n - 1);
  24. x.remove(x.size() - 1);
  25. }else {
  26. OutputOneSubsetBinary();
  27. OutputOneSubset();
  28. System.out.print("\n");
  29. }
  30. }
  31. private static void OutputOneSubset() {
  32. System.out.printf("; {");
  33. int k = 0;
  34. for (int i = x.size() - 1; i >=0; i--) {
  35. if (x.get(i) == 1) {
  36. if (k > 0)
  37. System.out.printf(",");
  38. System.out.printf("%d", x.size() - i);
  39. k++;
  40. }
  41. }
  42. System.out.printf("}");
  43. }
  44. private static void OutputOneSubsetBinary() {
  45. System.out.printf("%010d: ", ++cnt);
  46. for (int i = x.size() - 1; i >= 0; i--)
  47. System.out.printf("%d", x.get(i));
  48. }
  49. }

运行结果:

n:18(分钟)

image.png

n:19(分钟)

image.png

n:20(分钟)

image.png

n:21(分钟)

image.png

n:22(分钟)

image.png

n:23(分钟)

image.png

网上查的代码!

  1. class Main
  2. {
  3. static void printSubsets(String[] set)
  4. {
  5. int n = set.length;
  6. for (int i = 0; i < (1<<n); i++)
  7. {
  8. System.out.print("{ ");
  9. for (int j = 0; j < n; j++)
  10. if ((i & (1 << j)) > 0)
  11. System.out.print(set[j] + " ");
  12. System.out.println("}");
  13. }
  14. }
  15. public static void main(String[] args)
  16. {
  17. String[] set = {"1", "2", "3", "4",
  18. "5", "6", "7", "8"};
  19. printSubsets(set);
  20. }
  21. }