public static IList<IList<int>> Subsets(int[] nums)
{
List<IList<int>> result = new List<IList<int>>();
// Total number of subsets possible
int powerSetSize = (int)Math.Pow(2, nums.Length);
for (int i = 0; i < powerSetSize; ++i)
{
List<int> currSubSet = new List<int>();
for (int j = 0; j < nums.Length; ++j)
{
if ((i & (1 << j)) > 0)
{
currSubSet.Add(nums[j]);
}
}
result.Add(currSubSet);
}
return result;
}
No comments:
Post a Comment