马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
39.组合总和
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 全部 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限定重复被选取 。假如至少一个数字的被选数目不同,则两种组合是不同的。
对于给定的输入,包管和为 target 的不同组合数少于 150 个。
示例 1:
- <strong>输入:</strong>candidates = [2,3,6,7], target = 7
- <strong>输出:</strong>[[2,2,3],[7]]
- <strong>解释:</strong>
- 2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
- 7 也是一个候选, 7 = 7 。
- 仅有这两种组合。
复制代码 思绪:仍旧是利用溯回的模板,只需略改
- class Solution {
- public:
- vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
- vector<vector<int>> result; // 存储结果
- vector<int> path; // 当前路径
- 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++) {
- path.push_back(candidates[i]); // 选择当前数字
- backtracking(candidates, i, path, result, sum + candidates[i], target); // 递归下一层
- path.pop_back(); // 回溯,撤销选择
- }
- }
- };
复制代码 40.组合总和||
给定一个候选人编号的聚集 candidates 和一个目标数 target ,找出 candidates 中全部可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包罗重复的组合。
示例 1:
- <strong>输入:</strong> candidates = [10,1,2,7,6,1,5], target = 8,
- <strong>输出:</strong>
- [
- [1,1,6],
- [1,2,5],
- [1,7],
- [2,6]
- ]
复制代码 思绪:这个题目和上面那道题的差别在于:
(1)这个每个数字只能使用一次
只需:将递归的i改为i+1
- backtracking(candidates, i+1 , path, result, sum + candidates[i], target); // 递归下一层
复制代码 (2)不能包罗重复的组合
这个较难想到
先排序:
- sort(candidates.begin(), candidates.end()); // 排序,确保相同的元素在一起
-
复制代码 然后要确保这个两个雷同的数后面的数不要雷同
- // 去重:如果当前数字和前一个数字相同,并且前一个数字还没有被选择过,跳过当前数字
- if (i > index && candidates[i] == candidates[i - 1]) {
- continue;
- }
复制代码 完备代码:
- class Solution {public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { vector<vector<int>> result; // 存储结果 vector<int> path; // 当前路径 sort(candidates.begin(), candidates.end()); // 排序,确保相同的元素在一起
- 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++) { // 去重:如果当前数字和前一个数字相同,并且前一个数字还没有被选择过,跳过当前数字
- if (i > index && candidates[i] == candidates[i - 1]) {
- continue;
- } path.push_back(candidates[i]); // 选择当前数字 backtracking(candidates, i+1 , path, result, sum + candidates[i], target); // 递归下一层 path.pop_back(); // 回溯,撤销选择 } }};
复制代码 131.分割回文串
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是
回文串
。返回 s 全部可能的分割方案。
示例 1:
- <strong>输入:</strong>s = "aab"
- <strong>输出:</strong>[["a","a","b"],["aa","b"]]
复制代码 示例 2:
- <strong>输入:</strong>s = "a"
- <strong>输出:</strong>[["a"]]
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |