首页 > 编程语言 >代码随想录算法训练营第二十六天 | 39. 组合总和,40.组合总和II,131.分割回文串

代码随想录算法训练营第二十六天 | 39. 组合总和,40.组合总和II,131.分割回文串

时间:2023-02-12 00:33:05浏览次数:55  
标签:target 组合 int sum 随想录 vector candidates path 总和

一、参考资料

组合总和

题目链接/文章讲解:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html

视频讲解:https://www.bilibili.com/video/BV1KT4y1M7HJ

组合总和II

题目链接/文章讲解:https://programmercarl.com/0040.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8CII.html

视频讲解:https://www.bilibili.com/video/BV12V4y1V73A

分割回文串

题目链接/文章讲解:https://programmercarl.com/0131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.html

视频讲解:https://www.bilibili.com/video/BV1c54y1e7k6

二、LeetCode39. 组合总和

https://leetcode.cn/problems/combination-sum/description/

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []

提示:
1 <= candidates.length <= 30
2 <= candidates[i] <= 40
candidates 的所有元素 互不相同
1 <= target <= 40

本题搜索的过程抽象成树形结构如下:

注意图中叶子节点的返回条件,因为本题没有组合数量要求,仅仅是总和的限制,所以递归没有层数的限制,只要选取的元素总和超过target,就返回!

而在77.组合 (opens new window)216.组合总和III (opens new window)中都可以知道要递归K层,因为要取k个元素的组合。

我也能自己写出代码啦,模板真香

  1. class Solution {
  2. private:
  3. vector<int> path;
  4. vector<vector<int>> res;
  5. void backtracking(vector<int> candidates, int target, int sum, int startIndex) {
  6. if (sum > target) return ;
  7. if (sum == target) {
  8. res.push_back(path);
  9. return ;
  10. }
  11. for (int i = startIndex; i < candidates.size(); i++) {
  12. sum += candidates[i];
  13. path.push_back(candidates[i]);
  14. backtracking(candidates, target, sum, i); // 不用i+1了,表示可以重复读取当前的数
  15. path.pop_back();
  16. sum -= candidates[i];
  17. }
  18. }
  19. public:
  20. vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
  21. backtracking(candidates, target, 0, 0);
  22. return res;
  23. }
  24. };

剪枝优化(这里没有详细研究,大体思路还是在循环的地方提前终止):

对总集合排序之后,如果下一层的sum(就是本层的 sum + candidates[i])已经大于target,就可以结束本轮for循环的遍历。

  1. class Solution {
  2. private:
  3. vector<vector<int>> result;
  4. vector<int> path;
  5. void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
  6. if (sum == target) {
  7. result.push_back(path);
  8. return;
  9. }
  10. // 如果 sum + candidates[i] > target 就终止遍历
  11. for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
  12. sum += candidates[i];
  13. path.push_back(candidates[i]);
  14. backtracking(candidates, target, sum, i);
  15. sum -= candidates[i];
  16. path.pop_back();
  17. }
  18. }
  19. public:
  20. vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
  21. result.clear();
  22. path.clear();
  23. sort(candidates.begin(), candidates.end()); // 需要排序
  24. backtracking(candidates, target, 0, 0);
  25. return result;
  26. }
  27. };

在求和问题中,排序之后加剪枝是常见的套路!

三、LeetCode40.组合总和II

https://leetcode.cn/problems/combination-sum-ii/description/

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。

示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5, 输出: [ [1,2,2], [5] ]

提示:
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30

先排序!再树层去重

  • used[i - 1] == true,说明同一树枝candidates[i - 1]使用过

  • used[i - 1] == false,说明同一树层candidates[i - 1]使用过

  1. class Solution {
  2. private:
  3. vector<vector<int>> result;
  4. vector<int> path;
  5. void backtracking(vector<int>& candidates, int target, int sum, int startIndex, vector<bool>& used) {
  6. if (sum == target) {
  7. result.push_back(path);
  8. return;
  9. }
  10. for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
  11. // used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
  12. // used[i - 1] == false,说明同一树层candidates[i - 1]使用过
  13. // 要对同一树层使用过的元素进行跳过
  14. if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
  15. continue;
  16. }
  17. sum += candidates[i];
  18. path.push_back(candidates[i]);
  19. used[i] = true;
  20. backtracking(candidates, target, sum, i + 1, used); // 和39.组合总和的区别1,这里是i+1,每个数字在每个组合中只能使用一次
  21. used[i] = false;
  22. sum -= candidates[i];
  23. path.pop_back();
  24. }
  25. }
  26. public:
  27. vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
  28. vector<bool> used(candidates.size(), false);
  29. path.clear();
  30. result.clear();
  31. // 首先把给candidates排序,让其相同的元素都挨在一起。
  32. sort(candidates.begin(), candidates.end());
  33. backtracking(candidates, target, 0, 0, used);
  34. return result;
  35. }
  36. };

我写的如下~

  1. class Solution {
  2. private:
  3. vector<int> path;
  4. vector<vector<int>> res;
  5. void backtracking(vector<int> candidates, int target, int sum, int startIndex, vector<bool> used) {
  6. if (sum > target) return ;
  7. if (sum == target) {
  8. res.push_back(path);
  9. return ;
  10. }
  11. for (int i = startIndex; i < candidates.size(); i++) {
  12. if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) continue;
  13. sum += candidates[i];
  14. used[i] = true;
  15. path.push_back(candidates[i]);
  16. backtracking(candidates, target, sum, i + 1, used);
  17. path.pop_back();
  18. used[i] = false;
  19. sum -= candidates[i];
  20. }
  21. }
  22. public:
  23. vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
  24. vector<bool> used(candidates.size(), false);
  25. sort(candidates.begin(), candidates.end());
  26. backtracking(candidates, target, 0, 0, used);
  27. return res;
  28. }
  29. };

四、LeetCode131.分割回文串

https://leetcode.cn/problems/palindrome-partitioning/description/

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。

示例 1:
输入:s = "aab" 输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a" 输出:[["a"]]

提示:
1 <= s.length <= 16
s 仅由小写英文字母组成

带注释版:

  1. class Solution {
  2. private:
  3. vector<vector<string>> result;
  4. vector<string> path; // 放已经回文的子串
  5. void backtracking (const string& s, int startIndex) {
  6. // 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
  7. if (startIndex >= s.size()) {
  8. result.push_back(path);
  9. return;
  10. }
  11. for (int i = startIndex; i < s.size(); i++) {
  12. if (isPalindrome(s, startIndex, i)) { // 是回文子串
  13. // 获取[startIndex,i]在s中的子串
  14. string str = s.substr(startIndex, i - startIndex + 1);
  15. path.push_back(str);
  16. } else { // 不是回文,跳过
  17. continue;
  18. }
  19. backtracking(s, i + 1); // 寻找i+1为起始位置的子串
  20. path.pop_back(); // 回溯过程,弹出本次已经填在的子串
  21. }
  22. }
  23. bool isPalindrome(const string& s, int start, int end) {
  24. for (int i = start, j = end; i < j; i++, j--) {
  25. if (s[i] != s[j]) {
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
  31. public:
  32. vector<vector<string>> partition(string s) {
  33. result.clear();
  34. path.clear();
  35. backtracking(s, 0);
  36. return result;
  37. }
  38. };

优化后的代码就没详细学了

我能自己写出这个题的代码,已经觉得自己真的进步很大了:

  1. class Solution {
  2. private:
  3. vector<string> path;
  4. vector<vector<string>> res;
  5. bool isPalinedromic(string s, int begin, int end) {
  6. for (int i = begin, j = end; i < j; i++, j--) {
  7. if (s[i] != s[j]) return false;
  8. }
  9. return true;
  10. }
  11. void backtracking (string s, int startIndex) {
  12. if(startIndex > s.size()) return ;
  13. if (startIndex == s.size()) {
  14. res.push_back(path);
  15. return ;
  16. }
  17. for(int i = startIndex; i < s.size(); i++) {
  18. if(isPalinedromic(s, startIndex, i)) {
  19. string str = s.substr(startIndex, i - startIndex + 1);
  20. path.push_back(str);
  21. } else continue;
  22. backtracking(s, i + 1);
  23. path.pop_back();
  24. }
  25. }
  26. public:
  27. vector<vector<string>> partition(string s) {
  28. backtracking(s, 0);
  29. return res;
  30. }
  31. };

今日总结:

题目确实是当天写完的,只是博客没有及时写

  1. 感觉到自己真的进步好多了,能够理解懂视频的题目讲解,经历一些debug后还能尝试控制时间的前提下独立完成代码。

  1. 今天的难点应该挺多的,我花了很多时间理解来着,讲解回文串分割的视频应该看了两遍。

  1. 另外今天的组合数一个不太容易想到题解的“去重”问题,也是挺困扰,希望真的掌握后能做到举一反三呢

继续加油哈小赵~

标签:target,组合,int,sum,随想录,vector,candidates,path,总和
From: https://www.cnblogs.com/ucaszym/p/17113148.html

相关文章