mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-03 10:40:15 +08:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_backtrack {
|
||||
/* 回溯 */
|
||||
public void Backtrack(List<int> choices, int state, int n, List<int> res) {
|
||||
void Backtrack(List<int> choices, int state, int n, List<int> res) {
|
||||
// 当爬到第 n 阶时,方案数量加 1
|
||||
if (state == n)
|
||||
res[0]++;
|
||||
@@ -24,10 +24,10 @@ public class climbing_stairs_backtrack {
|
||||
}
|
||||
|
||||
/* 爬楼梯:回溯 */
|
||||
public int ClimbingStairsBacktrack(int n) {
|
||||
List<int> choices = new() { 1, 2 }; // 可选择向上爬 1 或 2 阶
|
||||
int ClimbingStairsBacktrack(int n) {
|
||||
List<int> choices = [1, 2]; // 可选择向上爬 1 或 2 阶
|
||||
int state = 0; // 从第 0 阶开始爬
|
||||
List<int> res = new() { 0 }; // 使用 res[0] 记录方案数量
|
||||
List<int> res = [0]; // 使用 res[0] 记录方案数量
|
||||
Backtrack(choices, state, n, res);
|
||||
return res[0];
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_constraint_dp {
|
||||
/* 带约束爬楼梯:动态规划 */
|
||||
public int ClimbingStairsConstraintDP(int n) {
|
||||
int ClimbingStairsConstraintDP(int n) {
|
||||
if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_dfs {
|
||||
/* 搜索 */
|
||||
public int DFS(int i) {
|
||||
int DFS(int i) {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2)
|
||||
return i;
|
||||
@@ -18,7 +18,7 @@ public class climbing_stairs_dfs {
|
||||
}
|
||||
|
||||
/* 爬楼梯:搜索 */
|
||||
public int ClimbingStairsDFS(int n) {
|
||||
int ClimbingStairsDFS(int n) {
|
||||
return DFS(n);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_dfs_mem {
|
||||
/* 记忆化搜索 */
|
||||
public int DFS(int i, int[] mem) {
|
||||
int DFS(int i, int[] mem) {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2)
|
||||
return i;
|
||||
@@ -23,7 +23,7 @@ public class climbing_stairs_dfs_mem {
|
||||
}
|
||||
|
||||
/* 爬楼梯:记忆化搜索 */
|
||||
public int ClimbingStairsDFSMem(int n) {
|
||||
int ClimbingStairsDFSMem(int n) {
|
||||
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
|
||||
int[] mem = new int[n + 1];
|
||||
Array.Fill(mem, -1);
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_dp {
|
||||
/* 爬楼梯:动态规划 */
|
||||
public int ClimbingStairsDP(int n) {
|
||||
int ClimbingStairsDP(int n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
// 初始化 dp 表,用于存储子问题的解
|
||||
@@ -24,7 +24,7 @@ public class climbing_stairs_dp {
|
||||
}
|
||||
|
||||
/* 爬楼梯:空间优化后的动态规划 */
|
||||
public int ClimbingStairsDPComp(int n) {
|
||||
int ClimbingStairsDPComp(int n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
int a = 1, b = 2;
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class coin_change {
|
||||
/* 零钱兑换:动态规划 */
|
||||
public int CoinChangeDP(int[] coins, int amt) {
|
||||
int CoinChangeDP(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
int MAX = amt + 1;
|
||||
// 初始化 dp 表
|
||||
@@ -33,7 +33,7 @@ public class coin_change {
|
||||
}
|
||||
|
||||
/* 零钱兑换:空间优化后的动态规划 */
|
||||
public int CoinChangeDPComp(int[] coins, int amt) {
|
||||
int CoinChangeDPComp(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
int MAX = amt + 1;
|
||||
// 初始化 dp 表
|
||||
@@ -57,7 +57,7 @@ public class coin_change {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] coins = { 1, 2, 5 };
|
||||
int[] coins = [1, 2, 5];
|
||||
int amt = 4;
|
||||
|
||||
// 动态规划
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class coin_change_ii {
|
||||
/* 零钱兑换 II:动态规划 */
|
||||
public int CoinChangeIIDP(int[] coins, int amt) {
|
||||
int CoinChangeIIDP(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n + 1, amt + 1];
|
||||
@@ -32,7 +32,7 @@ public class coin_change_ii {
|
||||
}
|
||||
|
||||
/* 零钱兑换 II:空间优化后的动态规划 */
|
||||
public int CoinChangeIIDPComp(int[] coins, int amt) {
|
||||
int CoinChangeIIDPComp(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[amt + 1];
|
||||
@@ -54,7 +54,7 @@ public class coin_change_ii {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] coins = { 1, 2, 5 };
|
||||
int[] coins = [1, 2, 5];
|
||||
int amt = 5;
|
||||
|
||||
// 动态规划
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class edit_distance {
|
||||
/* 编辑距离:暴力搜索 */
|
||||
public int EditDistanceDFS(string s, string t, int i, int j) {
|
||||
int EditDistanceDFS(string s, string t, int i, int j) {
|
||||
// 若 s 和 t 都为空,则返回 0
|
||||
if (i == 0 && j == 0)
|
||||
return 0;
|
||||
@@ -30,7 +30,7 @@ public class edit_distance {
|
||||
}
|
||||
|
||||
/* 编辑距离:记忆化搜索 */
|
||||
public int EditDistanceDFSMem(string s, string t, int[][] mem, int i, int j) {
|
||||
int EditDistanceDFSMem(string s, string t, int[][] mem, int i, int j) {
|
||||
// 若 s 和 t 都为空,则返回 0
|
||||
if (i == 0 && j == 0)
|
||||
return 0;
|
||||
@@ -56,7 +56,7 @@ public class edit_distance {
|
||||
}
|
||||
|
||||
/* 编辑距离:动态规划 */
|
||||
public int EditDistanceDP(string s, string t) {
|
||||
int EditDistanceDP(string s, string t) {
|
||||
int n = s.Length, m = t.Length;
|
||||
int[,] dp = new int[n + 1, m + 1];
|
||||
// 状态转移:首行首列
|
||||
@@ -82,7 +82,7 @@ public class edit_distance {
|
||||
}
|
||||
|
||||
/* 编辑距离:空间优化后的动态规划 */
|
||||
public int EditDistanceDPComp(string s, string t) {
|
||||
int EditDistanceDPComp(string s, string t) {
|
||||
int n = s.Length, m = t.Length;
|
||||
int[] dp = new int[m + 1];
|
||||
// 状态转移:首行
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class knapsack {
|
||||
/* 0-1 背包:暴力搜索 */
|
||||
public int KnapsackDFS(int[] weight, int[] val, int i, int c) {
|
||||
int KnapsackDFS(int[] weight, int[] val, int i, int c) {
|
||||
// 若已选完所有物品或背包无容量,则返回价值 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
@@ -25,7 +25,7 @@ public class knapsack {
|
||||
}
|
||||
|
||||
/* 0-1 背包:记忆化搜索 */
|
||||
public int KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {
|
||||
int KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {
|
||||
// 若已选完所有物品或背包无容量,则返回价值 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
@@ -47,7 +47,7 @@ public class knapsack {
|
||||
}
|
||||
|
||||
/* 0-1 背包:动态规划 */
|
||||
public int KnapsackDP(int[] weight, int[] val, int cap) {
|
||||
int KnapsackDP(int[] weight, int[] val, int cap) {
|
||||
int n = weight.Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n + 1, cap + 1];
|
||||
@@ -67,7 +67,7 @@ public class knapsack {
|
||||
}
|
||||
|
||||
/* 0-1 背包:空间优化后的动态规划 */
|
||||
public int KnapsackDPComp(int[] weight, int[] val, int cap) {
|
||||
int KnapsackDPComp(int[] weight, int[] val, int cap) {
|
||||
int n = weight.Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[cap + 1];
|
||||
@@ -89,8 +89,8 @@ public class knapsack {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] weight = { 10, 20, 30, 40, 50 };
|
||||
int[] val = { 50, 120, 150, 210, 240 };
|
||||
int[] weight = [10, 20, 30, 40, 50];
|
||||
int[] val = [50, 120, 150, 210, 240];
|
||||
int cap = 50;
|
||||
int n = weight.Length;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class min_cost_climbing_stairs_dp {
|
||||
/* 爬楼梯最小代价:动态规划 */
|
||||
public int MinCostClimbingStairsDP(int[] cost) {
|
||||
int MinCostClimbingStairsDP(int[] cost) {
|
||||
int n = cost.Length - 1;
|
||||
if (n == 1 || n == 2)
|
||||
return cost[n];
|
||||
@@ -25,7 +25,7 @@ public class min_cost_climbing_stairs_dp {
|
||||
}
|
||||
|
||||
/* 爬楼梯最小代价:空间优化后的动态规划 */
|
||||
public int MinCostClimbingStairsDPComp(int[] cost) {
|
||||
int MinCostClimbingStairsDPComp(int[] cost) {
|
||||
int n = cost.Length - 1;
|
||||
if (n == 1 || n == 2)
|
||||
return cost[n];
|
||||
@@ -40,7 +40,7 @@ public class min_cost_climbing_stairs_dp {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] cost = { 0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1 };
|
||||
int[] cost = [0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1];
|
||||
Console.WriteLine("输入楼梯的代价列表为");
|
||||
PrintUtil.PrintList(cost);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class min_path_sum {
|
||||
/* 最小路径和:暴力搜索 */
|
||||
public int MinPathSumDFS(int[][] grid, int i, int j) {
|
||||
int MinPathSumDFS(int[][] grid, int i, int j) {
|
||||
// 若为左上角单元格,则终止搜索
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
@@ -25,7 +25,7 @@ public class min_path_sum {
|
||||
}
|
||||
|
||||
/* 最小路径和:记忆化搜索 */
|
||||
public int MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
|
||||
int MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
|
||||
// 若为左上角单元格,则终止搜索
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
@@ -47,7 +47,7 @@ public class min_path_sum {
|
||||
}
|
||||
|
||||
/* 最小路径和:动态规划 */
|
||||
public int MinPathSumDP(int[][] grid) {
|
||||
int MinPathSumDP(int[][] grid) {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n, m];
|
||||
@@ -70,7 +70,7 @@ public class min_path_sum {
|
||||
}
|
||||
|
||||
/* 最小路径和:空间优化后的动态规划 */
|
||||
public int MinPathSumDPComp(int[][] grid) {
|
||||
int MinPathSumDPComp(int[][] grid) {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[m];
|
||||
@@ -94,12 +94,12 @@ public class min_path_sum {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[][] grid =
|
||||
{
|
||||
new int[4] { 1, 3, 1, 5 },
|
||||
new int[4] { 2, 2, 4, 2 },
|
||||
new int[4] { 5, 3, 2, 1 },
|
||||
new int[4] { 4, 3, 5, 2 }
|
||||
};
|
||||
[
|
||||
[1, 3, 1, 5],
|
||||
[2, 2, 4, 2],
|
||||
[5, 3, 2, 1],
|
||||
[4, 3, 5, 2]
|
||||
];
|
||||
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* 完全背包:动态规划 */
|
||||
public int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n + 1, cap + 1];
|
||||
@@ -28,7 +28,7 @@ public class unbounded_knapsack {
|
||||
}
|
||||
|
||||
/* 完全背包:空间优化后的动态规划 */
|
||||
public int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[cap + 1];
|
||||
@@ -49,8 +49,8 @@ public class unbounded_knapsack {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] wgt = { 1, 2, 3 };
|
||||
int[] val = { 5, 11, 15 };
|
||||
int[] wgt = [1, 2, 3];
|
||||
int[] val = [5, 11, 15];
|
||||
int cap = 4;
|
||||
|
||||
// 动态规划
|
||||
|
||||
Reference in New Issue
Block a user