List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> append = new ArrayList<Integer>();
for (int i = 0; i <= nums.length; i++){
combination(i, append, nums, result);
}
return result;
}
}
下面是利用位运算的方法解决这道题,非常巧妙。
Using Bit Manipulation
This is the most clever solution that I have seen. The idea is that to give all the possible subsets, we just need to exhaust all the possible combinations of the numbers. And each number has only two possibilities: either in or not in a subset. And this can be represented using a bit.
There is also another a way to visualize this idea. That is, if we use the above example, 1 appears once in every two consecutive subsets, 2 appears twice in every four consecutive subsets, and 3 appears four times in every eight subsets, shown in the following (initially the 8 subsets are all empty):