Skip to content

Commit 4d5346b

Browse files
Merge pull request #2377 from eeee0717/master
Update 0047.全排列2,添加C#
2 parents e929d41 + a937b76 commit 4d5346b

File tree

6 files changed

+203
-1
lines changed

6 files changed

+203
-1
lines changed

problems/0037.解数独.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,59 @@ object Solution {
756756
}
757757
}
758758
```
759+
### C#
760+
```csharp
761+
public class Solution
762+
{
763+
public void SolveSudoku(char[][] board)
764+
{
765+
BackTracking(board);
766+
}
767+
public bool BackTracking(char[][] board)
768+
{
769+
for (int i = 0; i < board.Length; i++)
770+
{
771+
for (int j = 0; j < board[0].Length; j++)
772+
{
773+
if (board[i][j] != '.') continue;
774+
for (char k = '1'; k <= '9'; k++)
775+
{
776+
if (IsValid(board, i, j, k))
777+
{
778+
board[i][j] = k;
779+
if (BackTracking(board)) return true;
780+
board[i][j] = '.';
781+
}
782+
}
783+
return false;
784+
}
785+
786+
}
787+
return true;
788+
}
789+
public bool IsValid(char[][] board, int row, int col, char val)
790+
{
791+
for (int i = 0; i < 9; i++)
792+
{
793+
if (board[i][col] == val) return false;
794+
}
795+
for (int i = 0; i < 9; i++)
796+
{
797+
if (board[row][i] == val) return false;
798+
}
799+
int startRow = (row / 3) * 3;
800+
int startCol = (col / 3) * 3;
801+
for (int i = startRow; i < startRow + 3; i++)
802+
{
803+
for (int j = startCol; j < startCol + 3; j++)
804+
{
805+
if (board[i][j] == val) return false;
806+
}
807+
}
808+
return true;
809+
}
810+
}
811+
```
759812

760813
<p align="center">
761814
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0047.全排列II.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,38 @@ object Solution {
521521
}
522522
}
523523
```
524+
### C#
525+
```csharp
526+
public class Solution
527+
{
528+
public List<IList<int>> res = new List<IList<int>>();
529+
public List<int> path = new List<int>();
530+
public IList<IList<int>> PermuteUnique(int[] nums)
531+
{
532+
Array.Sort(nums);
533+
BackTracking(nums, new bool[nums.Length]);
534+
return res;
535+
}
536+
public void BackTracking(int[] nums, bool[] used)
537+
{
538+
if (nums.Length == path.Count)
539+
{
540+
res.Add(new List<int>(path));
541+
return;
542+
}
543+
for (int i = 0; i < nums.Length; i++)
544+
{
545+
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) continue;
546+
if (used[i]) continue;
547+
path.Add(nums[i]);
548+
used[i] = true;
549+
BackTracking(nums, used);
550+
path.RemoveAt(path.Count - 1);
551+
used[i] = false;
552+
}
553+
}
554+
}
555+
```
524556

525557
<p align="center">
526558
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0051.N皇后.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,60 @@ object Solution {
865865
}
866866
}
867867
```
868+
### C#
869+
```csharp
870+
public class Solution
871+
{
872+
public List<IList<string>> res = new();
873+
public IList<IList<string>> SolveNQueens(int n)
874+
{
875+
char[][] chessBoard = new char[n][];
876+
for (int i = 0; i < n; i++)
877+
{
878+
chessBoard[i] = new char[n];
879+
for (int j = 0; j < n; j++)
880+
{
881+
chessBoard[i][j] = '.';
882+
}
883+
}
884+
BackTracking(n, 0, chessBoard);
885+
return res;
886+
}
887+
public void BackTracking(int n, int row, char[][] chessBoard)
888+
{
889+
if (row == n)
890+
{
891+
res.Add(chessBoard.Select(x => new string(x)).ToList());
892+
return;
893+
}
894+
for (int col = 0; col < n; col++)
895+
{
896+
if (IsValid(row, col, chessBoard, n))
897+
{
898+
chessBoard[row][col] = 'Q';
899+
BackTracking(n, row + 1, chessBoard);
900+
chessBoard[row][col] = '.';
901+
}
902+
}
903+
}
904+
public bool IsValid(int row, int col, char[][] chessBoard, int n)
905+
{
906+
for (int i = 0; i < row; i++)
907+
{
908+
if (chessBoard[i][col] == 'Q') return false;
909+
}
910+
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--)
911+
{
912+
if (chessBoard[i][j] == 'Q') return false;
913+
}
914+
for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++)
915+
{
916+
if (chessBoard[i][j] == 'Q') return false;
917+
}
918+
return true;
919+
}
920+
}
921+
```
868922

869923
<p align="center">
870924
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0053.最大子序和.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,26 @@ object Solution {
406406
}
407407
}
408408
```
409+
### C#
410+
**贪心**
411+
```csharp
412+
public class Solution
413+
{
414+
public int MaxSubArray(int[] nums)
415+
{
416+
int res = Int32.MinValue;
417+
int count = 0;
418+
for (int i = 0; i < nums.Length; i++)
419+
{
420+
count += nums[i];
421+
res = Math.Max(res, count);
422+
if (count < 0) count = 0;
423+
}
424+
return res;
425+
}
426+
}
427+
```
428+
409429

410430
<p align="center">
411431
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0376.摆动序列.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
在计算是否有峰值的时候,大家知道遍历的下标 i ,计算 prediff(nums[i] - nums[i-1]) 和 curdiff(nums[i+1] - nums[i]),如果`prediff < 0 && curdiff > 0` 或者 `prediff > 0 && curdiff < 0` 此时就有波动就需要统计。
6666

67-
这是我们思考本题的一个大题思路,但本题要考虑三种情况:
67+
这是我们思考本题的一个大体思路,但本题要考虑三种情况:
6868

6969
1. 情况一:上下坡中有平坡
7070
2. 情况二:数组首尾两端
@@ -692,6 +692,27 @@ object Solution {
692692
}
693693
}
694694
```
695+
### C#
696+
```csharp
697+
public class Solution
698+
{
699+
public int WiggleMaxLength(int[] nums)
700+
{
701+
if (nums.Length < 2) return nums.Length;
702+
int curDiff = 0, preDiff = 0, res = 1;
703+
for (int i = 0; i < nums.Length - 1; i++)
704+
{
705+
curDiff = nums[i + 1] - nums[i];
706+
if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0))
707+
{
708+
res++;
709+
preDiff = curDiff;
710+
}
711+
}
712+
return res;
713+
}
714+
}
715+
```
695716

696717
<p align="center">
697718
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0455.分发饼干.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,28 @@ object Solution {
378378
}
379379
}
380380
```
381+
### C#
382+
```csharp
383+
public class Solution
384+
{
385+
public int FindContentChildren(int[] g, int[] s)
386+
{
387+
Array.Sort(g);
388+
Array.Sort(s);
389+
int index = s.Length - 1;
390+
int res = 0;
391+
for (int i = g.Length - 1; i >=0; i--)
392+
{
393+
if(index >=0 && s[index]>=g[i])
394+
{
395+
res++;
396+
index--;
397+
}
398+
}
399+
return res;
400+
}
401+
}
402+
```
381403

382404
<p align="center">
383405
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
 (0)