-
Notifications
You must be signed in to change notification settings - Fork 30
/
Subsets.cs
executable file
·74 lines (70 loc) · 3.39 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Source : https://leetcode.com/problems/subsets/
// Author : codeyu
// Date : Tuesday, January 24, 2017 11:56:48 PM
/**********************************************************************************
*
*
* 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:
*
*
*
* [
* [3],
* [1],
* [2],
* [1,2,3],
* [1,3],
* [2,3],
* [1,2],
* []
* ]
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution078
{
public static IList<IList<int>> Subsets(int[] nums)
{
if (nums.Length == 0)
{
return null;
}
IList<int> list = new List<int>();
Array.Sort(nums);
return Helper(nums, nums.Length - 1);
}
private static IList<IList<int>> Helper(int[] num, int pos)
{
if (pos == -1)
{
IList<IList<int>> res = new List<IList<int>>();
IList<int> elem = new List<int>();
res.Add(elem);
return res;
}
else
{
IList<IList<int>> res = Helper(num, pos - 1);
int size = res.Count;
for (int i = 0; i < size; i++)
{
IList<int> elem = new List<int>(res[i]);
elem.Add(num[pos]);
res.Add(elem);
}
return res;
}
}
}
}