Sharing my bit manipulation in Java


  • 1
    C
    public class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        int index = 0;
        int length = nums.length;
        for(int i = 0; i < Math.pow(2, length); i++){
            List<Integer> set = new ArrayList<Integer>();
            //index = (index << 1) | 1;
            for(int j = 0; j < length; j++){
                if(((i>>j)&1) != 0){
                    set.add(nums[j]);
                }
            }
            result.add(set);
        }
        return result;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.