代码随想录26

打印 上一主题 下一主题

主题 1029|帖子 1029|积分 3087

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
39.组合总和

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

示例 1:
  1. <strong>输入:</strong>candidates = [2,3,6,7], target = 7
  2. <strong>输出:</strong>[[2,2,3],[7]]
  3. <strong>解释:</strong>
  4. 2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
  5. 7 也是一个候选, 7 = 7 。
  6. 仅有这两种组合。
复制代码
思绪:仍旧是利用溯回的模板,只需略改
  1. class Solution {
  2. public:
  3.     vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
  4.         vector<vector<int>> result;  // 存储结果
  5.         vector<int> path;            // 当前路径
  6.         backtracking(candidates, 0, path, result, 0, target);
  7.         return result;
  8.     }
  9.     void backtracking(vector<int>& candidates, int index, vector<int>& path, vector<vector<int>>& result, int sum, int target) {
  10.         // 终止条件:找到满足条件的组合
  11.         if (sum == target) {
  12.             result.push_back(path);  // 保存当前路径到结果集
  13.             return;
  14.         }
  15.         
  16.         // 如果当前和已经大于目标,终止该分支的递归,剪枝
  17.         if (sum > target) {
  18.             return;
  19.         }
  20.         // 遍历候选数
  21.         for (int i = index; i < candidates.size(); i++) {
  22.             path.push_back(candidates[i]);          // 选择当前数字
  23.             backtracking(candidates, i, path, result, sum + candidates[i], target);  // 递归下一层
  24.             path.pop_back();                        // 回溯,撤销选择
  25.         }
  26.     }
  27. };
复制代码
40.组合总和||

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

示例 1:
  1. <strong>输入:</strong> candidates = [10,1,2,7,6,1,5], target = 8,
  2. <strong>输出:</strong>
  3. [
  4. [1,1,6],
  5. [1,2,5],
  6. [1,7],
  7. [2,6]
  8. ]
复制代码
思绪:这个题目和上面那道题的差别在于:

(1)这个每个数字只能使用一次
只需:将递归的i改为i+1
  1. backtracking(candidates, i+1 , path, result, sum + candidates[i], target);  // 递归下一层
复制代码
(2)不能包罗重复的组合
这个较难想到
先排序:
 
  1.   sort(candidates.begin(), candidates.end());  // 排序,确保相同的元素在一起
复制代码
然后要确保这个两个雷同的数后面的数不要雷同
  1. // 去重:如果当前数字和前一个数字相同,并且前一个数字还没有被选择过,跳过当前数字
  2.             if (i > index && candidates[i] == candidates[i - 1]) {
  3.                 continue;
  4.             }
复制代码
完备代码:
  1. class Solution {public:    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {        vector<vector<int>> result;  // 存储结果        vector<int> path;            // 当前路径        sort(candidates.begin(), candidates.end());  // 排序,确保相同的元素在一起
  2.         backtracking(candidates, 0, path, result, 0, target);        return result;    }        void backtracking(vector<int>& candidates, int index, vector<int>& path, vector<vector<int>>& result, int sum, int target) {        // 终止条件:找到满意条件的组合        if (sum == target) {            result.push_back(path);  // 保存当前路径到结果集            return;        }                // 假如当前和已经大于目标,终止该分支的递归,剪枝        if (sum > target) {            return;        }        // 遍历候选数        for (int i = index; i < candidates.size(); i++) {            // 去重:如果当前数字和前一个数字相同,并且前一个数字还没有被选择过,跳过当前数字
  3.             if (i > index && candidates[i] == candidates[i - 1]) {
  4.                 continue;
  5.             }                        path.push_back(candidates[i]);          // 选择当前数字            backtracking(candidates, i+1 , path, result, sum + candidates[i], target);  // 递归下一层            path.pop_back();                        // 回溯,撤销选择        }    }};
复制代码
131.分割回文串

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 
回文串
 。返回 s 全部可能的分割方案。

示例 1:
  1. <strong>输入:</strong>s = "aab"
  2. <strong>输出:</strong>[["a","a","b"],["aa","b"]]
复制代码
示例 2:
  1. <strong>输入:</strong>s = "a"
  2. <strong>输出:</strong>[["a"]]
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

干翻全岛蛙蛙

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表