Leetcode 78: Subsets

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 78: Subsets相关的知识,希望对你有一定的参考价值。

Given a set of distinct integers, nums, return all possible subsets (the power set).

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],
  []
]

 1 public class Solution {
 2     public IList<IList<int>> Subsets(int[] nums) {
 3         var results = new List<IList<int>>();
 4         results.Add(new List<int>());
 5         DFS(nums, 0, results);
 6         return results;
 7     }
 8     
 9     private void DFS(int[] nums, int start, IList<IList<int>> results)
10     {
11         if (start >= nums.Length)
12         {
13             return;
14         }
15         
16         int count = results.Count;
17         
18         for (int i = 0; i < count; i++)
19         {
20             var r = new List<int>(results[i]);
21             r.Add(nums[start]);
22             results.Add(r);
23         }
24         
25         DFS(nums, start + 1, results);
26     }
27 }

 




以上是关于Leetcode 78: Subsets的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 78: Subsets

Leetcode 78. Subsets

Leetcode 78. Subsets

[leetcode-78-Subsets]

Leetcode 78: Subsets

LeetCode78. Subsets