From f134c39c868c3b8a48389db6e11fe412904087b2 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 22 Dec 2023 10:17:21 +0800 Subject: [PATCH 1/7] =?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; + } + } +} +```

From 69103f6c86fddc3d10ffe921dce8b93e259f71e2 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 23 Dec 2023 10:11:06 +0800 Subject: [PATCH 2/7] =?UTF-8?q?Update0051.N=E7=9A=87=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0051.N皇后.md | 54 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index f6290793..1e108540 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -865,6 +865,60 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public List> res = new(); + public IList> SolveNQueens(int n) + { + char[][] chessBoard = new char[n][]; + for (int i = 0; i < n; i++) + { + chessBoard[i] = new char[n]; + for (int j = 0; j < n; j++) + { + chessBoard[i][j] = '.'; + } + } + BackTracking(n, 0, chessBoard); + return res; + } + public void BackTracking(int n, int row, char[][] chessBoard) + { + if (row == n) + { + res.Add(chessBoard.Select(x => new string(x)).ToList()); + return; + } + for (int col = 0; col < n; col++) + { + if (IsValid(row, col, chessBoard, n)) + { + chessBoard[row][col] = 'Q'; + BackTracking(n, row + 1, chessBoard); + chessBoard[row][col] = '.'; + } + } + } + public bool IsValid(int row, int col, char[][] chessBoard, int n) + { + for (int i = 0; i < row; i++) + { + if (chessBoard[i][col] == 'Q') return false; + } + for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) + { + if (chessBoard[i][j] == 'Q') return false; + } + for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) + { + if (chessBoard[i][j] == 'Q') return false; + } + return true; + } +} +```

From 3bd387df7cde50b6fada28aa824841f4c842921d Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 24 Dec 2023 09:42:46 +0800 Subject: [PATCH 3/7] =?UTF-8?q?Update0037.=E8=A7=A3=E6=95=B0=E7=8B=AC?= =?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/0037.解数独.md | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/0037.解数独.md b/problems/0037.解数独.md index 1763063e..d96e59df 100644 --- a/problems/0037.解数独.md +++ b/problems/0037.解数独.md @@ -756,6 +756,59 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public void SolveSudoku(char[][] board) + { + BackTracking(board); + } + public bool BackTracking(char[][] board) + { + for (int i = 0; i < board.Length; i++) + { + for (int j = 0; j < board[0].Length; j++) + { + if (board[i][j] != '.') continue; + for (char k = '1'; k <= '9'; k++) + { + if (IsValid(board, i, j, k)) + { + board[i][j] = k; + if (BackTracking(board)) return true; + board[i][j] = '.'; + } + } + return false; + } + + } + return true; + } + public bool IsValid(char[][] board, int row, int col, char val) + { + for (int i = 0; i < 9; i++) + { + if (board[i][col] == val) return false; + } + for (int i = 0; i < 9; i++) + { + if (board[row][i] == val) return false; + } + int startRow = (row / 3) * 3; + int startCol = (col / 3) * 3; + for (int i = startRow; i < startRow + 3; i++) + { + for (int j = startCol; j < startCol + 3; j++) + { + if (board[i][j] == val) return false; + } + } + return true; + } +} +```

From 9cea1567fe338ec4842276c4e27e9d4933893bd3 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 25 Dec 2023 09:35:23 +0800 Subject: [PATCH 4/7] =?UTF-8?q?Update0455.=E5=88=86=E5=8F=91=E9=A5=BC?= =?UTF-8?q?=E5=B9=B2=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/0455.分发饼干.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index c9c1a852..778adc94 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -378,6 +378,28 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int FindContentChildren(int[] g, int[] s) + { + Array.Sort(g); + Array.Sort(s); + int index = s.Length - 1; + int res = 0; + for (int i = g.Length - 1; i >=0; i--) + { + if(index >=0 && s[index]>=g[i]) + { + res++; + index--; + } + } + return res; + } +} +```

From b01155fbf24ee11b8a45aa9b7e0f67f2398509c2 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 26 Dec 2023 09:11:30 +0800 Subject: [PATCH 5/7] =?UTF-8?q?docs:=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 5203d7d6..ceea31fe 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -64,7 +64,7 @@ 在计算是否有峰值的时候,大家知道遍历的下标 i ,计算 prediff(nums[i] - nums[i-1]) 和 curdiff(nums[i+1] - nums[i]),如果`prediff < 0 && curdiff > 0` 或者 `prediff > 0 && curdiff < 0` 此时就有波动就需要统计。 -这是我们思考本题的一个大题思路,但本题要考虑三种情况: +这是我们思考本题的一个大体思路,但本题要考虑三种情况: 1. 情况一:上下坡中有平坡 2. 情况二:数组首尾两端 From 7924803206a0dc2b06b6d6f3c7281d9f56970a71 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 26 Dec 2023 09:12:03 +0800 Subject: [PATCH 6/7] =?UTF-8?q?Update0376.=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97=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/0376.摆动序列.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index ceea31fe..5c2241c8 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -692,6 +692,27 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int WiggleMaxLength(int[] nums) + { + if (nums.Length < 2) return nums.Length; + int curDiff = 0, preDiff = 0, res = 1; + for (int i = 0; i < nums.Length - 1; i++) + { + curDiff = nums[i + 1] - nums[i]; + if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) + { + res++; + preDiff = curDiff; + } + } + return res; + } +} +```

From a937b7674623ecbbe24e61d20d6af7d771ab6304 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 27 Dec 2023 09:49:34 +0800 Subject: [PATCH 7/7] =?UTF-8?q?Update0053.=E6=9C=80=E5=A4=A7=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E8=B4=AA?= =?UTF-8?q?=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0053.最大子序和.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 639c54bc..78c8b382 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -406,6 +406,26 @@ object Solution { } } ``` +### C# +**贪心** +```csharp +public class Solution +{ + public int MaxSubArray(int[] nums) + { + int res = Int32.MinValue; + int count = 0; + for (int i = 0; i < nums.Length; i++) + { + count += nums[i]; + res = Math.Max(res, count); + if (count < 0) count = 0; + } + return res; + } +} +``` +