Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2cd982f

Browse files
committed
Update0040.组合总和2,添加C#
1 parent d542a27 commit 2cd982f

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

‎problems/0040.组合总和II.md‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,39 @@ object Solution {
773773
}
774774
}
775775
```
776-
776+
### C#
777+
```csharp
778+
public class Solution
779+
{
780+
public List<IList<int>> res = new List<IList<int>>();
781+
public List<int> path = new List<int>();
782+
public IList<IList<int>> CombinationSum2(int[] candidates, int target)
783+
{
784+
785+
Array.Sort(candidates);
786+
BackTracking(candidates, target, 0, 0);
787+
return res;
788+
}
789+
public void BackTracking(int[] candidates, int target, int start, int sum)
790+
{
791+
if (sum > target) return;
792+
if (sum == target)
793+
{
794+
res.Add(new List<int>(path));
795+
return;
796+
}
797+
for (int i = start; i < candidates.Length && sum + candidates[i] <= target; i++)
798+
{
799+
if (i > start && candidates[i] == candidates[i - 1]) continue;
800+
sum += candidates[i];
801+
path.Add(candidates[i]);
802+
BackTracking(candidates, target, i + 1, sum);
803+
sum -= candidates[i];
804+
path.RemoveAt(path.Count - 1);
805+
}
806+
}
807+
}
808+
```
777809
<p align="center">
778810
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
779811
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /