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