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; + } +} +```

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; + } + } +} +```

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; + } +} +```

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; + } +} +``` +

diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 5203d7d6..5c2241c8 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. 情况二:数组首尾两端 @@ -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; + } +} +```

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; + } +} +```