📜  使用位操作打印子集数组中所有唯一子集的Java程序

📅  最后修改于: 2022-05-13 01:55:28.751000             🧑  作者: Mango

使用位操作打印子集数组中所有唯一子集的Java程序

给定一个唯一元素的整数数组,返回所有可能的子集(幂集)。解决方案集不能包含重复的子集,并且应该以任何顺序返回解决方案。

插图:

Input: array = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Input: array = [0]
Output: [[],[0]]

方法一

算法

  1. 生成所有可能的长度为 n 的二进制位掩码。
  2. 将子集映射到每个位掩码
    • 位掩码中第 i 个位置的 1 表示子集中存在 nums[i]
    • 0 表示不存在。
  3. 返回输出列表。

执行:



例子

Java
// Java Program to Print all unique subsets in an array of
// subsets using bit manipulation
  
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
  
// Main class
class GFG {
  
    // Method 1
    // Helper method
    static List > subsets(int[] nums)
    {
        List > output = new ArrayList<>();
        int n = nums.length;
  
        for (int i = (int)Math.pow(2, n);
             i < (int)Math.pow(2, n + 1); ++i) {
  
            // Generate bitmask, from 0..00 to 1..11
            String bitmask
                = Integer.toBinaryString(i).substring(1);
  
            // Appending subset corresponding to that
            // bitmask
            List curr = new ArrayList<>();
  
            for (int j = 0; j < n; ++j) {
                if (bitmask.charAt(j) == '1')
  
                    // Adding it to subset
                    curr.add(nums[j]);
            }
  
            // Adding the subset
            output.add(curr);
        }
        return output;
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input integer array
        int arr[] = { 1, 2, 3 };
  
        // Calling method 1 in main() method
        List > output = subsets(arr);
  
        // Printing all unique subsets in an array
        System.out.println(output);
    }
}


Java
// Java Program to Print all unique subsets in an array of
// subsets using bit manipulation
  
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
  
// Main class
class GFG {
  
    // Method 1
    static List > subsets(int[] nums)
    {
  
        // Creating List class object
        // Declaring. object of integer List
        List > output = new ArrayList<>();
  
        int n = nums.length;
  
        // Increase the size by using left shift (1 * 2^n)
        int size = 1 << n;
  
        for (int i = 0; i < size; i++) {
            List curr = new ArrayList<>();
            for (int j = 0; j < n; j++) {
  
                // right shift i and j i.e. i/2^j
                if (((i >> j) & 1) == 1) {
  
                    // Add it to subset
                    curr.add(nums[j]);
                }
            }
  
            // Adding the subset
            output.add(curr);
        }
        return output;
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input array
        int arr[] = { 1, 2, 3 };
  
        // Calling method 1 in main() method
        List > output = subsets(arr);
  
        // Print all unique subsets in an array
        System.out.println(output);
    }
}


输出
[[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]

复杂度分析:

  • 时间复杂度:- O(N x 2^N) 生成所有子集,然后将它们复制到输出列表中。
  • 空间复杂度:- O(N x 2^N) 保留长度为 NN 的所有子集,因为每个 NN 元素可能存在或不存在。

方法二:

例子

Java

// Java Program to Print all unique subsets in an array of
// subsets using bit manipulation
  
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
  
// Main class
class GFG {
  
    // Method 1
    static List > subsets(int[] nums)
    {
  
        // Creating List class object
        // Declaring. object of integer List
        List > output = new ArrayList<>();
  
        int n = nums.length;
  
        // Increase the size by using left shift (1 * 2^n)
        int size = 1 << n;
  
        for (int i = 0; i < size; i++) {
            List curr = new ArrayList<>();
            for (int j = 0; j < n; j++) {
  
                // right shift i and j i.e. i/2^j
                if (((i >> j) & 1) == 1) {
  
                    // Add it to subset
                    curr.add(nums[j]);
                }
            }
  
            // Adding the subset
            output.add(curr);
        }
        return output;
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input array
        int arr[] = { 1, 2, 3 };
  
        // Calling method 1 in main() method
        List > output = subsets(arr);
  
        // Print all unique subsets in an array
        System.out.println(output);
    }
}
输出
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]