From f134c39c868c3b8a48389db6e11fe412904087b2 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 22 Dec 2023 10:17:21 +0800 Subject: [PATCH] =?UTF-8?q?Update=200047.=E5=85=A8=E6=8E=92=E5=88=972?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0047.全排列II.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index 4fed8a5c..7f2c3638 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -521,6 +521,38 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public List> res = new List>(); + public List path = new List(); + public IList> PermuteUnique(int[] nums) + { + Array.Sort(nums); + BackTracking(nums, new bool[nums.Length]); + return res; + } + public void BackTracking(int[] nums, bool[] used) + { + if (nums.Length == path.Count) + { + res.Add(new List(path)); + return; + } + for (int i = 0; i < nums.Length; i++) + { + if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) continue; + if (used[i]) continue; + path.Add(nums[i]); + used[i] = true; + BackTracking(nums, used); + path.RemoveAt(path.Count - 1); + used[i] = false; + } + } +} +```