Content-Length: 561755 | pFad | http://github.com/BigEggStudy/LeetCode-CS/commit/19497ea23cd7afefb3b7c062703b04113d0775ec

36 Add solution for problem 216 · BigEggStudy/LeetCode-CS@19497ea · GitHub
Skip to content

Commit 19497ea

Browse files
committed
Add solution for problem 216
1 parent e99d8f1 commit 19497ea

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System.Collections.Generic;
3+
4+
namespace LeetCode.Test
5+
{
6+
[TestClass]
7+
public class _0216_CombinationSumIII_Test
8+
{
9+
[TestMethod]
10+
public void CombinationSum3_1()
11+
{
12+
var solution = new _0216_CombinationSumIII();
13+
var result = solution.CombinationSum3(3, 7);
14+
AssertHelper.AssertList(new List<IList<int>>()
15+
{
16+
new int[] { 1, 2, 4},
17+
}, result);
18+
}
19+
20+
[TestMethod]
21+
public void CombinationSum3_2()
22+
{
23+
var solution = new _0216_CombinationSumIII();
24+
var result = solution.CombinationSum3(3, 9);
25+
AssertHelper.AssertList(new List<IList<int>>()
26+
{
27+
new int[] { 1, 2, 6 },
28+
new int[] { 1, 3, 5 },
29+
new int[] { 2, 3, 4 },
30+
}, result);
31+
}
32+
33+
[TestMethod]
34+
public void CombinationSum3_3()
35+
{
36+
var solution = new _0216_CombinationSumIII();
37+
var result = solution.CombinationSum3(4, 1);
38+
AssertHelper.AssertList(new List<IList<int>>()
39+
{
40+
}, result);
41+
}
42+
43+
[TestMethod]
44+
public void CombinationSum3_4()
45+
{
46+
var solution = new _0216_CombinationSumIII();
47+
var result = solution.CombinationSum3(3, 2);
48+
AssertHelper.AssertList(new List<IList<int>>()
49+
{
50+
}, result);
51+
}
52+
53+
[TestMethod]
54+
public void CombinationSum3_5()
55+
{
56+
var solution = new _0216_CombinationSumIII();
57+
var result = solution.CombinationSum3(9, 45);
58+
AssertHelper.AssertList(new List<IList<int>>()
59+
{
60+
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
61+
}, result);
62+
}
63+
}
64+
}

LeetCode.Test/LeetCode.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
<Compile Include="0201-0250\0211-AddAndSearchWordDataStructureDesign-Test.cs" />
235235
<Compile Include="0201-0250\0212-WordSearchII-Test.cs" />
236236
<Compile Include="0201-0250\0215-KthLargestElementInAnArray-Test.cs" />
237+
<Compile Include="0201-0250\0216-CombinationSumIII-Test.cs" />
237238
<Compile Include="0201-0250\0217-ContainsDuplicate-Test.cs" />
238239
<Compile Include="0201-0250\0218-TheSkylineProblem-Test.cs" />
239240
<Compile Include="0201-0250\0219-ContainsDuplicateII-Test.cs" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//-----------------------------------------------------------------------------
2+
// Runtime: 200ms
3+
// Memory Usage: 25.8 MB
4+
// Link: https://leetcode.com/submissions/detail/394731091/
5+
//-----------------------------------------------------------------------------
6+
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
10+
namespace LeetCode
11+
{
12+
public class _0216_CombinationSumIII
13+
{
14+
public IList<IList<int>> CombinationSum3(int k, int n)
15+
{
16+
var results = new List<IList<int>>();
17+
Helper(0, k, n, new List<int>(), results);
18+
return results;
19+
}
20+
21+
private void Helper(int index, int k, int n, IList<int> current, IList<IList<int>> results)
22+
{
23+
if (current.Count == k && n == 0)
24+
{
25+
results.Add(current);
26+
return;
27+
}
28+
if (index >= k || n < 0) return;
29+
30+
var start = index == 0 ? 1 : current.Last() + 1;
31+
for (int i = start; i <= n && i < 10; i++)
32+
{
33+
var newList = new List<int>(current);
34+
newList.Add(i);
35+
Helper(index + 1, k, n - i, newList, results);
36+
}
37+
}
38+
}
39+
}

LeetCode/LeetCode.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
<Compile Include="0201-0250\0211-AddAndSearchWordDataStructureDesign.cs" />
226226
<Compile Include="0201-0250\0212-WordSearchII.cs" />
227227
<Compile Include="0201-0250\0215-KthLargestElementInAnArray.cs" />
228+
<Compile Include="0201-0250\0216-CombinationSumIII.cs" />
228229
<Compile Include="0201-0250\0217-ContainsDuplicate.cs" />
229230
<Compile Include="0201-0250\0218-TheSkylineProblem.cs" />
230231
<Compile Include="0201-0250\0219-ContainsDuplicateII.cs" />

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![AppVeyor Build Status](https://img.shields.io/appveyor/build/bigegg/leetcode-cs?label=Windows%20Build%20Status&logo=AppVeyor&style=flat-square)](https://ci.appveyor.com/project/BigEgg/leetcode-cs)
2-
[![Solved Problems](https://img.shields.io/badge/Solved%20Problems-811-blue.svg?style=flat-square)](https://github.com/BigEggStudy/LeetCode-CS)
2+
[![Solved Problems](https://img.shields.io/badge/Solved%20Problems-812-blue.svg?style=flat-square)](https://github.com/BigEggStudy/LeetCode-CS)
33

44
# LeetCode
55
The C# solutions for LeetCode problems.
@@ -250,6 +250,7 @@ The C# solutions for LeetCode problems.
250250
| 211 | Add and Search Word - Data structure design | [C#](./LeetCode/0201-0250/0211-AddAndSearchWordDataStructureDesign.cs)(272ms) | O(1) | O(N) | |
251251
| 212 | Word Search II | [C#](./LeetCode/0201-0250/0212-WordSearchII.cs)(272ms) | O(N*M) | O(K) | |
252252
| 215 | Kth Largest Element in an Array | [C#](./LeetCode/0201-0250/0215-KthLargestElementInAnArray.cs)(100ms) | O(NLogN) | O(1) | |
253+
| 216 | Combination Sum III | [C#](./LeetCode/0201-0250/0216-CombinationSumIII.cs)(200ms) | O(9! * K / (9-K)!) | O(K) | |
253254
| 217 | Contains Duplicate | [C#](./LeetCode/0201-0250/0217-ContainsDuplicate.cs)(112ms) | O(N) | O(N) | |
254255
| 218 | The Skyline Problem | [C#](./LeetCode/0201-0250/0218-TheSkylineProblem.cs)(288ms) | O(NLogN) | O(N) | |
255256
| 219 | Contains Duplicate II | [C#](./LeetCode/0201-0250/0219-ContainsDuplicateII.cs)(100ms) | O(N) | O(N) | |

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/BigEggStudy/LeetCode-CS/commit/19497ea23cd7afefb3b7c062703b04113d0775ec

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy