Merge pull request #2377 from eeee0717/master

Update 0047.全排列2,添加C#
This commit is contained in:
程序员Carl
2023-12-28 09:51:58 +08:00
committed by GitHub
6 changed files with 203 additions and 1 deletions

View File

@ -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;
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -521,6 +521,38 @@ object Solution {
}
}
```
### C#
```csharp
public class Solution
{
public List<IList<int>> res = new List<IList<int>>();
public List<int> path = new List<int>();
public IList<IList<int>> 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<int>(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;
}
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -865,6 +865,60 @@ object Solution {
}
}
```
### C#
```csharp
public class Solution
{
public List<IList<string>> res = new();
public IList<IList<string>> 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;
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -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;
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -64,7 +64,7 @@
在计算是否有峰值的时候,大家知道遍历的下标 i ,计算 prediffnums[i] - nums[i-1] 和 curdiffnums[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;
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -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;
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">