hello-algo/codes/csharp/chapter_backtracking/subset_sum_i_naive.cs
hpstory f62256bee1
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list)

* Modify method name to PascalCase(backtracking)

* Modify method name to PascalCase(computational complexity)

* Modify method name to PascalCase(divide and conquer)

* Modify method name to PascalCase(dynamic programming)

* Modify method name to PascalCase(graph)

* Modify method name to PascalCase(greedy)

* Modify method name to PascalCase(hashing)

* Modify method name to PascalCase(heap)

* Modify method name to PascalCase(searching)

* Modify method name to PascalCase(sorting)

* Modify method name to PascalCase(stack and queue)

* Modify method name to PascalCase(tree)

* local check
2023-10-07 12:33:46 -05:00

53 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* File: subset_sum_i_naive.cs
* Created Time: 2023-06-25
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_backtracking;
public class subset_sum_i_naive {
/* 回溯算法:子集和 I */
public static void Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
// 子集和等于 target 时,记录解
if (total == target) {
res.Add(new List<int>(state));
return;
}
// 遍历所有选择
for (int i = 0; i < choices.Length; i++) {
// 剪枝:若子集和超过 target ,则跳过该选择
if (total + choices[i] > target) {
continue;
}
// 尝试:做出选择,更新元素和 total
state.Add(choices[i]);
// 进行下一轮选择
Backtrack(state, target, total + choices[i], choices, res);
// 回退:撤销选择,恢复到之前的状态
state.RemoveAt(state.Count - 1);
}
}
/* 求解子集和 I包含重复子集 */
public static List<List<int>> SubsetSumINaive(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
int total = 0; // 子集和
List<List<int>> res = new(); // 结果列表(子集列表)
Backtrack(state, target, total, nums, res);
return res;
}
[Test]
public void Test() {
int[] nums = { 3, 4, 5 };
int target = 9;
List<List<int>> res = SubsetSumINaive(nums, target);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
Console.WriteLine("所有和等于 " + target + " 的子集 res = ");
foreach (var subset in res) {
PrintUtil.PrintList(subset);
}
Console.WriteLine("请注意,该方法输出的结果包含重复集合");
}
}