diff --git a/codes/cpp/chapter_backtracking/subset_sum_i.cpp b/codes/cpp/chapter_backtracking/subset_sum_i.cpp new file mode 100644 index 000000000..d241deda9 --- /dev/null +++ b/codes/cpp/chapter_backtracking/subset_sum_i.cpp @@ -0,0 +1,57 @@ +/** + * File: subset_sum_i.cpp + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +#include "../utils/common.hpp" + +/* 回溯算法:子集和 I */ +void backtrack(vector &state, int target, vector &choices, int start, vector> &res) { + // 子集和等于 target 时,记录解 + if (target == 0) { + res.push_back(state); + return; + } + // 遍历所有选择 + // 剪枝二:从 start 开始遍历,避免生成重复子集 + for (int i = start; i < choices.size(); i++) { + // 剪枝一:若子集和超过 target ,则直接结束循环 + // 这是因为数组已排序,后边元素更大,子集和一定超过 target + if (target - choices[i] < 0) { + break; + } + // 尝试:做出选择,更新 target, start + state.push_back(choices[i]); + // 进行下一轮选择 + backtrack(state, target - choices[i], choices, i, res); + // 回退:撤销选择,恢复到之前的状态 + state.pop_back(); + } +} + +/* 求解子集和 I */ +vector> subsetSumI(vector &nums, int target) { + vector state; // 状态(子集) + sort(nums.begin(), nums.end()); // 对 nums 进行排序 + int start = 0; // 遍历起始点 + vector> res; // 结果列表(子集列表) + backtrack(state, target, nums, start, res); + return res; +} + +/* Driver Code */ +int main() { + vector nums = {3, 4, 5}; + int target = 9; + + vector> res = subsetSumI(nums, target); + + cout << "输入数组 nums = "; + printVector(nums); + cout << "target = " << target << endl; + cout << "所有和等于 " << target << " 的子集 res = " << endl; + printVectorMatrix(res); + + return 0; +} diff --git a/codes/cpp/chapter_backtracking/subset_sum_i_naive.cpp b/codes/cpp/chapter_backtracking/subset_sum_i_naive.cpp new file mode 100644 index 000000000..326dc6313 --- /dev/null +++ b/codes/cpp/chapter_backtracking/subset_sum_i_naive.cpp @@ -0,0 +1,54 @@ +/** + * File: subset_sum_i_naive.cpp + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +#include "../utils/common.hpp" + +/* 回溯算法:子集和 I */ +void backtrack(vector &state, int target, int total, vector &choices, vector> &res) { + // 子集和等于 target 时,记录解 + if (total == target) { + res.push_back(state); + return; + } + // 遍历所有选择 + for (size_t i = 0; i < choices.size(); i++) { + // 剪枝:若子集和超过 target ,则跳过该选择 + if (total + choices[i] > target) { + continue; + } + // 尝试:做出选择,更新元素和 total + state.push_back(choices[i]); + // 进行下一轮选择 + backtrack(state, target, total + choices[i], choices, res); + // 回退:撤销选择,恢复到之前的状态 + state.pop_back(); + } +} + +/* 求解子集和 I(包含重复子集) */ +vector> subsetSumINaive(vector &nums, int target) { + vector state; // 状态(子集) + int total = 0; // 子集和 + vector> res; // 结果列表(子集列表) + backtrack(state, target, total, nums, res); + return res; +} + +/* Driver Code */ +int main() { + vector nums = {3, 4, 5}; + int target = 9; + + vector> res = subsetSumINaive(nums, target); + + cout << "输入数组 nums = "; + printVector(nums); + cout << "target = " << target << endl; + cout << "所有和等于 " << target << " 的子集 res = " << endl; + printVectorMatrix(res); + + return 0; +} diff --git a/codes/cpp/chapter_backtracking/subset_sum_ii.cpp b/codes/cpp/chapter_backtracking/subset_sum_ii.cpp new file mode 100644 index 000000000..5c335cb4b --- /dev/null +++ b/codes/cpp/chapter_backtracking/subset_sum_ii.cpp @@ -0,0 +1,62 @@ +/** + * File: subset_sum_ii.cpp + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +#include "../utils/common.hpp" + +/* 回溯算法:子集和 II */ +void backtrack(vector &state, int target, vector &choices, int start, vector> &res) { + // 子集和等于 target 时,记录解 + if (target == 0) { + res.push_back(state); + return; + } + // 遍历所有选择 + // 剪枝二:从 start 开始遍历,避免生成重复子集 + // 剪枝三:从 start 开始遍历,避免重复选择同一元素 + for (int i = start; i < choices.size(); i++) { + // 剪枝一:若子集和超过 target ,则直接结束循环 + // 这是因为数组已排序,后边元素更大,子集和一定超过 target + if (target - choices[i] < 0) { + break; + } + // 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过 + if (i > start && choices[i] == choices[i - 1]) { + continue; + } + // 尝试:做出选择,更新 target, start + state.push_back(choices[i]); + // 进行下一轮选择 + backtrack(state, target - choices[i], choices, i + 1, res); + // 回退:撤销选择,恢复到之前的状态 + state.pop_back(); + } +} + +/* 求解子集和 II */ +vector> subsetSumII(vector &nums, int target) { + vector state; // 状态(子集) + sort(nums.begin(), nums.end()); // 对 nums 进行排序 + int start = 0; // 遍历起始点 + vector> res; // 结果列表(子集列表) + backtrack(state, target, nums, start, res); + return res; +} + +/* Driver Code */ +int main() { + vector nums = {4, 4, 5}; + int target = 9; + + vector> res = subsetSumII(nums, target); + + cout << "输入数组 nums = "; + printVector(nums); + cout << "target = " << target << endl; + cout << "所有和等于 " << target << " 的子集 res = " << endl; + printVectorMatrix(res); + + return 0; +} diff --git a/codes/java/chapter_backtracking/subset_sum_i.java b/codes/java/chapter_backtracking/subset_sum_i.java new file mode 100644 index 000000000..47377bc0c --- /dev/null +++ b/codes/java/chapter_backtracking/subset_sum_i.java @@ -0,0 +1,55 @@ +/** + * File: subset_sum_i.java + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +package chapter_backtracking; + +import java.util.*; + +public class subset_sum_i { + /* 回溯算法:子集和 I */ + static void backtrack(List state, int target, int[] choices, int start, List> res) { + // 子集和等于 target 时,记录解 + if (target == 0) { + res.add(new ArrayList<>(state)); + return; + } + // 遍历所有选择 + // 剪枝二:从 start 开始遍历,避免生成重复子集 + for (int i = start; i < choices.length; i++) { + // 剪枝一:若子集和超过 target ,则直接结束循环 + // 这是因为数组已排序,后边元素更大,子集和一定超过 target + if (target - choices[i] < 0) { + break; + } + // 尝试:做出选择,更新 target, start + state.add(choices[i]); + // 进行下一轮选择 + backtrack(state, target - choices[i], choices, i, res); + // 回退:撤销选择,恢复到之前的状态 + state.remove(state.size() - 1); + } + } + + /* 求解子集和 I */ + static List> subsetSumI(int[] nums, int target) { + List state = new ArrayList<>(); // 状态(子集) + Arrays.sort(nums); // 对 nums 进行排序 + int start = 0; // 遍历起始点 + List> res = new ArrayList<>(); // 结果列表(子集列表) + backtrack(state, target, nums, start, res); + return res; + } + + public static void main(String[] args) { + int[] nums = { 3, 4, 5 }; + int target = 9; + + List> res = subsetSumI(nums, target); + + System.out.println("输入数组 nums = " + Arrays.toString(nums) + ", target = " + target); + System.out.println("所有和等于 " + target + " 的子集 res = " + res); + } +} diff --git a/codes/java/chapter_backtracking/subset_sum_i_naive.java b/codes/java/chapter_backtracking/subset_sum_i_naive.java new file mode 100644 index 000000000..15b7075e0 --- /dev/null +++ b/codes/java/chapter_backtracking/subset_sum_i_naive.java @@ -0,0 +1,53 @@ +/** + * File: subset_sum_i_naive.java + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +package chapter_backtracking; + +import java.util.*; + +public class subset_sum_i_naive { + /* 回溯算法:子集和 I */ + static void backtrack(List state, int target, int total, int[] choices, List> res) { + // 子集和等于 target 时,记录解 + if (total == target) { + res.add(new ArrayList<>(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.remove(state.size() - 1); + } + } + + /* 求解子集和 I(包含重复子集) */ + static List> subsetSumINaive(int[] nums, int target) { + List state = new ArrayList<>(); // 状态(子集) + int total = 0; // 子集和 + List> res = new ArrayList<>(); // 结果列表(子集列表) + backtrack(state, target, total, nums, res); + return res; + } + + public static void main(String[] args) { + int[] nums = { 3, 4, 5 }; + int target = 9; + + List> res = subsetSumINaive(nums, target); + + System.out.println("输入数组 nums = " + Arrays.toString(nums) + ", target = " + target); + System.out.println("所有和等于 " + target + " 的子集 res = " + res); + System.out.println("请注意,该方法输出的结果包含重复集合"); + } +} diff --git a/codes/java/chapter_backtracking/subset_sum_ii.java b/codes/java/chapter_backtracking/subset_sum_ii.java new file mode 100644 index 000000000..5d72fb439 --- /dev/null +++ b/codes/java/chapter_backtracking/subset_sum_ii.java @@ -0,0 +1,60 @@ +/** + * File: subset_sum_ii.java + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +package chapter_backtracking; + +import java.util.*; + +public class subset_sum_ii { + /* 回溯算法:子集和 II */ + static void backtrack(List state, int target, int[] choices, int start, List> res) { + // 子集和等于 target 时,记录解 + if (target == 0) { + res.add(new ArrayList<>(state)); + return; + } + // 遍历所有选择 + // 剪枝二:从 start 开始遍历,避免生成重复子集 + // 剪枝三:从 start 开始遍历,避免重复选择同一元素 + for (int i = start; i < choices.length; i++) { + // 剪枝一:若子集和超过 target ,则直接结束循环 + // 这是因为数组已排序,后边元素更大,子集和一定超过 target + if (target - choices[i] < 0) { + break; + } + // 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过 + if (i > start && choices[i] == choices[i - 1]) { + continue; + } + // 尝试:做出选择,更新 target, start + state.add(choices[i]); + // 进行下一轮选择 + backtrack(state, target - choices[i], choices, i + 1, res); + // 回退:撤销选择,恢复到之前的状态 + state.remove(state.size() - 1); + } + } + + /* 求解子集和 II */ + static List> subsetSumII(int[] nums, int target) { + List state = new ArrayList<>(); // 状态(子集) + Arrays.sort(nums); // 对 nums 进行排序 + int start = 0; // 遍历起始点 + List> res = new ArrayList<>(); // 结果列表(子集列表) + backtrack(state, target, nums, start, res); + return res; + } + + public static void main(String[] args) { + int[] nums = { 4, 4, 5 }; + int target = 9; + + List> res = subsetSumII(nums, target); + + System.out.println("输入数组 nums = " + Arrays.toString(nums) + ", target = " + target); + System.out.println("所有和等于 " + target + " 的子集 res = " + res); + } +} diff --git a/codes/python/chapter_backtracking/subset_sum_i.py b/codes/python/chapter_backtracking/subset_sum_i.py new file mode 100644 index 000000000..cab0b395e --- /dev/null +++ b/codes/python/chapter_backtracking/subset_sum_i.py @@ -0,0 +1,48 @@ +""" +File: subset_sum_i.py +Created Time: 2023-06-17 +Author: Krahets (krahets@163.com) +""" + + +def backtrack( + state: list[int], target: int, choices: list[int], start: int, res: list[list[int]] +): + """回溯算法:子集和 I""" + # 子集和等于 target 时,记录解 + if target == 0: + res.append(list(state)) + return + # 遍历所有选择 + # 剪枝二:从 start 开始遍历,避免生成重复子集 + for i in range(start, len(choices)): + # 剪枝一:若子集和超过 target ,则直接结束循环 + # 这是因为数组已排序,后边元素更大,子集和一定超过 target + if target - choices[i] < 0: + break + # 尝试:做出选择,更新 target, start + state.append(choices[i]) + # 进行下一轮选择 + backtrack(state, target - choices[i], choices, i, res) + # 回退:撤销选择,恢复到之前的状态 + state.pop() + + +def subset_sum_i(nums: list[int], target: int) -> list[list[int]]: + """求解子集和 I""" + state = [] # 状态(子集) + nums.sort() # 对 nums 进行排序 + start = 0 # 遍历起始点 + res = [] # 结果列表(子集列表) + backtrack(state, target, nums, start, res) + return res + + +"""Driver Code""" +if __name__ == "__main__": + nums = [3, 4, 5] + target = 9 + res = subset_sum_i(nums, target) + + print(f"输入数组 nums = {nums}, target = {target}") + print(f"所有和等于 {target} 的子集 res = {res}") diff --git a/codes/python/chapter_backtracking/subset_sum_i_naive.py b/codes/python/chapter_backtracking/subset_sum_i_naive.py new file mode 100644 index 000000000..4c19265de --- /dev/null +++ b/codes/python/chapter_backtracking/subset_sum_i_naive.py @@ -0,0 +1,50 @@ +""" +File: subset_sum_i_naive.py +Created Time: 2023-06-17 +Author: Krahets (krahets@163.com) +""" + + +def backtrack( + state: list[int], + target: int, + total: int, + choices: list[int], + res: list[list[int]], +): + """回溯算法:子集和 I""" + # 子集和等于 target 时,记录解 + if total == target: + res.append(list(state)) + return + # 遍历所有选择 + for i in range(len(choices)): + # 剪枝:若子集和超过 target ,则跳过该选择 + if total + choices[i] > target: + continue + # 尝试:做出选择,更新元素和 total + state.append(choices[i]) + # 进行下一轮选择 + backtrack(state, target, total + choices[i], choices, res) + # 回退:撤销选择,恢复到之前的状态 + state.pop() + + +def subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]: + """求解子集和 I(包含重复子集)""" + state = [] # 状态(子集) + total = 0 # 子集和 + res = [] # 结果列表(子集列表) + backtrack(state, target, total, nums, res) + return res + + +"""Driver Code""" +if __name__ == "__main__": + nums = [3, 4, 5] + target = 9 + res = subset_sum_i_naive(nums, target) + + print(f"输入数组 nums = {nums}, target = {target}") + print(f"所有和等于 {target} 的子集 res = {res}") + print(f"请注意,该方法输出的结果包含重复集合") diff --git a/codes/python/chapter_backtracking/subset_sum_ii.py b/codes/python/chapter_backtracking/subset_sum_ii.py new file mode 100644 index 000000000..130f0b321 --- /dev/null +++ b/codes/python/chapter_backtracking/subset_sum_ii.py @@ -0,0 +1,52 @@ +""" +File: subset_sum_ii.py +Created Time: 2023-06-17 +Author: Krahets (krahets@163.com) +""" + + +def backtrack( + state: list[int], target: int, choices: list[int], start: int, res: list[list[int]] +): + """回溯算法:子集和 II""" + # 子集和等于 target 时,记录解 + if target == 0: + res.append(list(state)) + return + # 遍历所有选择 + # 剪枝二:从 start 开始遍历,避免生成重复子集 + # 剪枝三:从 start 开始遍历,避免重复选择同一元素 + for i in range(start, len(choices)): + # 剪枝一:若子集和超过 target ,则直接结束循环 + # 这是因为数组已排序,后边元素更大,子集和一定超过 target + if target - choices[i] < 0: + break + # 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过 + if i > start and choices[i] == choices[i - 1]: + continue + # 尝试:做出选择,更新 target, start + state.append(choices[i]) + # 进行下一轮选择 + backtrack(state, target - choices[i], choices, i + 1, res) + # 回退:撤销选择,恢复到之前的状态 + state.pop() + + +def subset_sum_ii(nums: list[int], target: int) -> list[list[int]]: + """求解子集和 II""" + state = [] # 状态(子集) + nums.sort() # 对 nums 进行排序 + start = 0 # 遍历起始点 + res = [] # 结果列表(子集列表) + backtrack(state, target, nums, start, res) + return res + + +"""Driver Code""" +if __name__ == "__main__": + nums = [4, 4, 5] + target = 9 + res = subset_sum_ii(nums, target) + + print(f"输入数组 nums = {nums}, target = {target}") + print(f"所有和等于 {target} 的子集 res = {res}") diff --git a/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i.png b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i.png new file mode 100644 index 000000000..f001c0a14 Binary files /dev/null and b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i.png differ diff --git a/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i_naive.png b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i_naive.png new file mode 100644 index 000000000..aa7a6996e Binary files /dev/null and b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i_naive.png differ diff --git a/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i_pruning.png b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i_pruning.png new file mode 100644 index 000000000..d1b8240c8 Binary files /dev/null and b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_i_pruning.png differ diff --git a/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_ii.png b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_ii.png new file mode 100644 index 000000000..27f67e720 Binary files /dev/null and b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_ii.png differ diff --git a/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_ii_repeat.png b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_ii_repeat.png new file mode 100644 index 000000000..0d1f28186 Binary files /dev/null and b/docs/chapter_backtracking/subset_sum_problem.assets/subset_sum_ii_repeat.png differ diff --git a/docs/chapter_backtracking/subset_sum_problem.md b/docs/chapter_backtracking/subset_sum_problem.md new file mode 100644 index 000000000..068d2ec0b --- /dev/null +++ b/docs/chapter_backtracking/subset_sum_problem.md @@ -0,0 +1,324 @@ +# 子集和问题 + +!!! question + + 给定一个正整数数组 `nums` 和一个目标正整数 `target` ,请找出所有可能的组合,使得组合中的元素和等于 `target` 。给定数组无重复元素,每个元素可以被选取多次。请以列表形式返回这些组合,列表中不应包含重复组合。 + +例如,输入集合 $\{3, 4, 5\}$ 和目标整数 $9$ ,由于集合中的数字可以被重复选取,因此解为 $\{3, 3, 3\}, \{4, 5\}$ 。请注意,子集是不区分元素顺序的,例如 $\{4, 5\}$ 和 $\{5, 4\}$ 是同一个子集。 + +## 参考全排列解法 + +从回溯算法的角度看,我们可以把子集的生成过程想象成一系列选择的结果,并在选择过程中实时更新“元素和”,当元素和等于 `target` 时,就将子集记录至结果列表。 + +与上节全排列问题不同的是,本题允许重复选取同一元素,因此无需借助 `selected` 布尔列表来记录元素是否已被选择。我们可以对全排列代码进行小幅修改,初步得到解题代码。 + +=== "Java" + + ```java title="subset_sum_i_naive.java" + [class]{subset_sum_i_naive}-[func]{backtrack} + + [class]{subset_sum_i_naive}-[func]{subsetSumINaive} + ``` + +=== "C++" + + ```cpp title="subset_sum_i_naive.cpp" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumINaive} + ``` + +=== "Python" + + ```python title="subset_sum_i_naive.py" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subset_sum_i_naive} + ``` + +=== "Go" + + ```go title="subset_sum_i_naive.go" + [class]{}-[func]{backtrackINaive} + + [class]{}-[func]{subsetSumINaive} + ``` + +=== "JavaScript" + + ```javascript title="subset_sum_i_naive.js" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumINaive} + ``` + +=== "TypeScript" + + ```typescript title="subset_sum_i_naive.ts" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumINaive} + ``` + +=== "C" + + ```c title="subset_sum_i_naive.c" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumINaive} + ``` + +=== "C#" + + ```csharp title="subset_sum_i_naive.cs" + [class]{subset_sum_i_naive}-[func]{backtrack} + + [class]{subset_sum_i_naive}-[func]{subsetSumINaive} + ``` + +=== "Swift" + + ```swift title="subset_sum_i_naive.swift" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumINaive} + ``` + +=== "Zig" + + ```zig title="subset_sum_i_naive.zig" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumINaive} + ``` + +=== "Dart" + + ```dart title="subset_sum_i_naive.dart" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumINaive} + ``` + +向以上代码输入数组 $[3, 4, 5]$ 和目标元素 $9$ ,输出结果为 $[3, 3, 3], [4, 5], [5, 4]$ 。**虽然成功找出了所有和为 $9$ 的子集,但其中存在重复的子集 $[4, 5]$ 和 $[5, 4]$** 。这是因为搜索过程是区分选择顺序的,如下图所示,先选 $4$ 后选 $5$ 与先选 $5$ 后选 $4$ 是两种不同的情况。 + +![子集搜索与越界剪枝](subset_sum_problem.assets/subset_sum_i_naive.png) + +## 重复子集剪枝 + +为了去除重复子集,**一种直接的思路是对结果列表进行去重**。但这个方法效率很低,因为: + +- 当数组元素较多,尤其是当 `target` 较大时,搜索过程会产生大量的重复子集。 +- 比较子集(数组)的异同是很耗时的,需要先排序数组,再比较数组中每个元素的异同。 + +为了达到最佳效率,**我们希望在搜索过程中通过剪枝进行去重**。观察下图,重复子集是在以不同顺序选择数组元素时产生的,具体来看: + +1. 第一轮和第二轮分别选择 $3$ , $4$ ,会生成包含这两个元素的所有子集,记为 $[3, 4, \cdots]$ 。 +2. 若第一轮选择 $4$ ,**则第二轮应该跳过 $3$** ,因为该选择产生的子集 $[4, 3, \cdots]$ 和 `1.` 中提到的子集完全重复。 +3. 同理,若第一轮选择 $5$ ,**则第二轮应该跳过 $3$ 和 $4$** ,因为子集 $[5, 3, \cdots]$ 和子集 $[5, 4, \cdots]$ 和之前的子集重复。 + +![不同选择顺序导致的重复子集](subset_sum_problem.assets/subset_sum_i_pruning.png) + +总结来看,给定输入数组 $[x_1, x_2, \cdots, x_n]$ ,设搜索过程中的选择序列为 $[x_{i_1}, x_{i_2}, \cdots , x_{i_m}]$ ,则该选择序列需要满足 $i_1 \leq i_2 \leq \cdots \leq i_m$ 。**不满足该条件的选择序列都是重复子集**。 + +为实现该剪枝,我们初始化变量 `start` ,用于指示遍历起点。**当做出选择 $x_{i}$ 后,设定下一轮从索引 $i$ 开始遍历**,从而完成子集去重。 + +除此之外,我们还对代码进行了两项优化。首先,我们在开启搜索前将数组 `nums` 排序,在搜索过程中,**当子集和超过 `target` 时直接结束循环**,因为后边的元素更大,其子集和都一定会超过 `target` 。其次,**我们通过在 `target` 上执行减法来统计元素和**,当 `target` 等于 $0$ 时记录解,省去了元素和变量 `total` 。 + +=== "Java" + + ```java title="subset_sum_i.java" + [class]{subset_sum_i}-[func]{backtrack} + + [class]{subset_sum_i}-[func]{subsetSumI} + ``` + +=== "C++" + + ```cpp title="subset_sum_i.cpp" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumI} + ``` + +=== "Python" + + ```python title="subset_sum_i.py" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subset_sum_i} + ``` + +=== "Go" + + ```go title="subset_sum_i.go" + [class]{}-[func]{backtrackI} + + [class]{}-[func]{subsetSumI} + ``` + +=== "JavaScript" + + ```javascript title="subset_sum_i.js" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumI} + ``` + +=== "TypeScript" + + ```typescript title="subset_sum_i.ts" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumI} + ``` + +=== "C" + + ```c title="subset_sum_i.c" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumI} + ``` + +=== "C#" + + ```csharp title="subset_sum_i.cs" + [class]{subset_sum_i}-[func]{backtrack} + + [class]{subset_sum_i}-[func]{subsetSumI} + ``` + +=== "Swift" + + ```swift title="subset_sum_i.swift" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumI} + ``` + +=== "Zig" + + ```zig title="subset_sum_i.zig" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumI} + ``` + +=== "Dart" + + ```dart title="subset_sum_i.dart" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumI} + ``` + +如下图所示,为将数组 $[3, 4, 5]$ 和目标元素 $9$ 输入到以上代码后的整体回溯过程。 + +![子集和 I 回溯过程](subset_sum_problem.assets/subset_sum_i.png) + +## 考虑相等元素 + +!!! question + + 给定一个正整数数组 `nums` 和一个目标正整数 `target` ,请找出所有可能的组合,使得组合中的元素和等于 `target` 。**给定数组可能包含重复元素,每个元素只可被选择一次**。请以列表形式返回这些组合,列表中不应包含重复组合。 + +相比于上题,**本题的输入数组可能包含重复元素**,这引入了新的问题。例如,给定数组 $[4, \hat{4}, 5]$ 和目标元素 $9$ ,则现有代码的输出结果为 $[4, 5], [\hat{4}, 5]$ ,也出现了重复子集。**造成这种重复的原因是相等元素在某轮中被多次选择**。如下图所示,第一轮共有三个选择,其中两个都为 $4$ ,会产生两个重复的搜索分支,从而输出重复子集;同理,第二轮的两个 $4$ 也会产生重复子集。 + +![相等元素导致的重复子集](subset_sum_problem.assets/subset_sum_ii_repeat.png) + +为解决此问题,**我们需要限制相等元素在每一轮中只被选择一次**。实现方式比较巧妙:由于数组是已排序的,因此相等元素都是相邻的。利用该特性,在某轮选择中,若当前元素与其左边元素相等,则说明它已经被选择过,因此直接跳过当前元素。 + +与此同时,**本题规定数组元素只能被选择一次**。幸运的是,我们也可以利用变量 `start` 来满足该约束:当做出选择 $x_{i}$ 后,设定下一轮从索引 $i + 1$ 开始向后遍历。这样即能去除重复子集,也能避免重复选择相等元素。 + +=== "Java" + + ```java title="subset_sum_ii.java" + [class]{subset_sum_ii}-[func]{backtrack} + + [class]{subset_sum_ii}-[func]{subsetSumII} + ``` + +=== "C++" + + ```cpp title="subset_sum_ii.cpp" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumII} + ``` + +=== "Python" + + ```python title="subset_sum_ii.py" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subset_sum_ii} + ``` + +=== "Go" + + ```go title="subset_sum_ii.go" + [class]{}-[func]{backtrackII} + + [class]{}-[func]{subsetSumII} + ``` + +=== "JavaScript" + + ```javascript title="subset_sum_ii.js" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumII} + ``` + +=== "TypeScript" + + ```typescript title="subset_sum_ii.ts" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumII} + ``` + +=== "C" + + ```c title="subset_sum_ii.c" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumII} + ``` + +=== "C#" + + ```csharp title="subset_sum_ii.cs" + [class]{subset_sum_ii}-[func]{backtrack} + + [class]{subset_sum_ii}-[func]{subsetSumII} + ``` + +=== "Swift" + + ```swift title="subset_sum_ii.swift" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumII} + ``` + +=== "Zig" + + ```zig title="subset_sum_ii.zig" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumII} + ``` + +=== "Dart" + + ```dart title="subset_sum_ii.dart" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subsetSumII} + ``` + +下图展示了数组 $[4, 4, 5]$ 和目标元素 $9$ 的回溯过程,共包含四种剪枝操作。建议你将图示与代码注释相结合,理解整个搜索过程,以及每种剪枝操作是如何工作的。 + +![子集和 II 回溯过程](subset_sum_problem.assets/subset_sum_ii.png) diff --git a/mkdocs.yml b/mkdocs.yml index d7853ec2b..f1aab8487 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -187,20 +187,20 @@ nav: - 9.4.   小结: chapter_graph/summary.md - 10.     搜索: - chapter_searching/index.md - - 10.1.   二分查找(New): chapter_searching/binary_search.md - - 10.2.   二分查找边界(New): chapter_searching/binary_search_edge.md + - 10.1.   二分查找: chapter_searching/binary_search.md + - 10.2.   二分查找边界: chapter_searching/binary_search_edge.md - 10.3.   哈希优化策略: chapter_searching/replace_linear_by_hashing.md - 10.4.   重识搜索算法: chapter_searching/searching_algorithm_revisited.md - 10.5.   小结: chapter_searching/summary.md - 11.     排序: - chapter_sorting/index.md - 11.1.   排序算法: chapter_sorting/sorting_algorithm.md - - 11.2.   选择排序(New): chapter_sorting/selection_sort.md + - 11.2.   选择排序: chapter_sorting/selection_sort.md - 11.3.   冒泡排序: chapter_sorting/bubble_sort.md - 11.4.   插入排序: chapter_sorting/insertion_sort.md - 11.5.   快速排序: chapter_sorting/quick_sort.md - 11.6.   归并排序: chapter_sorting/merge_sort.md - - 11.7.   堆排序(New): chapter_sorting/heap_sort.md + - 11.7.   堆排序: chapter_sorting/heap_sort.md - 11.8.   桶排序: chapter_sorting/bucket_sort.md - 11.9.   计数排序: chapter_sorting/counting_sort.md - 11.10.   基数排序: chapter_sorting/radix_sort.md @@ -209,7 +209,8 @@ nav: - chapter_backtracking/index.md - 12.1.   回溯算法: chapter_backtracking/backtracking_algorithm.md - 12.2.   全排列问题: chapter_backtracking/permutations_problem.md - - 12.3.   N 皇后问题: chapter_backtracking/n_queens_problem.md + - 12.3.   子集和问题(New): chapter_backtracking/subset_sum_problem.md + - 12.4.   N 皇后问题: chapter_backtracking/n_queens_problem.md - 13.     附录: - 13.1.   编程环境安装: chapter_appendix/installation.md - 13.2.   一起参与创作: chapter_appendix/contribution.md