078 Subsets
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3]
, a solution is:


思路
类似于排列组合的问题，我们都可以一律利用回溯的方法来解决。在回溯的函数里，我们递归地调用函数本身，layers代表回溯的层数；nums是输入的数组，append是当前层已有的数，result是将要返回的最终结果。当回溯层数为0时，我们可以将当前层已有的数加入到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):


The code is as follows.

