forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
078-Subsets.cs
38 lines (33 loc) · 1.07 KB
/
078-Subsets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//-----------------------------------------------------------------------------
// Runtime: 232ms
// Memory Usage: 30.6 MB
// Link: https://leetcode.com/submissions/detail/365290998/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _078_Subsets
{
public IList<IList<int>> Subsets(int[] nums)
{
var results = new List<IList<int>>();
results.Add(new List<int>());
if (nums == null || nums.Length == 0) { return results; }
Array.Sort(nums);
int i, j, n = nums.Length, number = 1 << nums.Length, temp;
for (i = 1; i < number; i++)
{
var result = new List<int>();
temp = i;
for (j = 0; j < n; j++)
{
if ((temp & 1) == 1) { result.Add(nums[j]); }
temp >>= 1;
}
results.Add(result);
}
return results;
}
}
}