Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gokul.demo.Success;
- import java.util.ArrayList;
- import java.util.List;
- public class SubsetBit {
- public static void main(String[] args) {
- SubsetBit sub = new SubsetBit();
- int[] arr = new int[] { 1, 2, 3, 4 };
- sub.subsets(arr);
- }
- List<List<Integer>> opList = new ArrayList<>();
- public List<List<Integer>> subsets(int[] nums) {
- for (int i = 0; i < Math.pow(2, nums.length); i++) {
- opList.add(getBinary(i, nums.length, nums));
- }
- return opList;
- }
- private List<Integer> getBinary(int num, int size, int[] nums) {
- List<Integer> minList = new ArrayList<>();
- String binaryString = Integer.toBinaryString(num);
- while (binaryString.length() < size) {
- binaryString = "0" + binaryString;
- }
- char[] charArray = binaryString.toCharArray();
- for (int i = 0; i < charArray.length; i++) {
- if ('1' == charArray[i]) {
- minList.add(nums[i]);
- }
- }
- return minList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement