代码随想录day23 ||39组合总和1 40组合总和2 131分割回文串
创始人
2024-11-19 05:34:44
0

39组合总和1

力扣题目链接

题目描述:

给你一个 无重复元素 的整数数组 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 输出: []

代码(自己的想法)

class Solution { public:     vector> result;     vector path;     int sumfunc(vector path){         int sum=0;         for(auto i:path){             sum+=i;         }         return sum;     }     void backtracking(int startindex,int target,vector& candidates){      if(sumfunc(path)>target){         return;      }      if(sumfunc(path)==target){         result.push_back(path);         return;      }      for(int i=startindex;i> combinationSum(vector& candidates, int target) {         backtracking(0,target,candidates);         return result;     } };

更官方的代码:

class Solution { public:     vector> result;     vector path;         void backtracking(int startindex,int target,vector& candidates,int sum){      if(sum>target){         return;      }      if(sum==target){         result.push_back(path);         return;      }      for(int i=startindex;i> combinationSum(vector& candidates, int target) {         int sum=0;         backtracking(0,target,candidates,sum);         return result;     } };

40组合总和2

力扣题目链接

题目描述:

给定一个候选人编号的集合 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] ] 

代码:

class Solution { private:     vector> result;     vector path;     void backtracking(vector& candidates, int target, int sum, int startIndex, vector& used) {         if (sum == target) {             result.push_back(path);             return;         }         for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {             // used[i - 1] == true,说明同一树枝candidates[i - 1]使用过             // used[i - 1] == false,说明同一树层candidates[i - 1]使用过             // 要对同一树层使用过的元素进行跳过             if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {                 continue;             }             sum += candidates[i];             path.push_back(candidates[i]);             used[i] = true;             backtracking(candidates, target, sum, i + 1, used); // 和39.组合总和的区别1,这里是i+1,每个数字在每个组合中只能使用一次             used[i] = false;             sum -= candidates[i];             path.pop_back();         }     }  public:     vector> combinationSum2(vector& candidates, int target) {         vector used(candidates.size(), false);         path.clear();         result.clear();         // 首先把给candidates排序,让其相同的元素都挨在一起。         sort(candidates.begin(), candidates.end());         backtracking(candidates, target, 0, 0, used);         return result;     } };

131分割回文串

力扣题目链接

题目描述:

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 

回文串

 。返回 s 所有可能的分割方案。

示例 1:

输入:s = "aab" 输出:[["a","a","b"],["aa","b"]] 

示例 2:

输入:s = "a" 输出:[["a"]]
class Solution  {  private:       vector> result; // 存放最终结果       vector path; // 存放当前回文子串        // 回溯函数,参数为原始字符串和当前起始位置       void backtracking (const string& s, int startIndex)  {          // 如果起始位置已经大于原始字符串的大小,说明已经找到了一组分割方案           if (startIndex >= s.size()) {               // 将当前 path 存入 result               result.push_back(path);               return;           }         // 循环所有可能的结束位置           for (int i = startIndex; i < s.size(); i++)  {              // 判断是否是回文子串               if (isPalindrome(s, startIndex, i))  {  // 是回文子串                   // 获取[startIndex,i]在原始字符串中的子串                   string str = s.substr(startIndex, i - startIndex + 1);                   // 将子串存入 path                   path.push_back(str);  }             else {                                // 不是回文,跳过                   continue; }                          // 递归回溯,寻找下一个回文子串               backtracking(s, i + 1);               // 回溯过程,弹出本次已经添加的子串               path.pop_back();                }     // 判断是否是回文       bool isPalindrome(const string& s, int start, int end)  {          for (int i = start, j = end; i < j; i++, j--)                if (s[i] != s[j])                    return false;                                 return true;      } public:       // 分割字符串       vector> partition(string s)  {          result.clear();           path.clear();           backtracking(s, 0);           return result;       } };  

相关内容

热门资讯

重大来袭!新道游app辅助器(... 重大来袭!新道游app辅助器(辅助挂)必赢教程(2026版作弊开挂辅助挂);大家肯定在之前新道游ap...
必看攻略!打两圈怀疑有外挂(透... 必看攻略!打两圈怀疑有外挂(透视)微扑克教程(细节作弊开挂辅助软件);打两圈怀疑有外挂最新版本免费下...
玩家必看科普!uuganmes... 玩家必看科普!uuganmes辅助器(辅助挂)细节方法(必赢作弊开挂辅助黑科技);uuganmes辅...
普及知识!哈糖大菠萝可以开挂(... 您好:哈糖大菠萝可以开挂这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的...
攻略讲解!红茶馆app辅助(透... 攻略讲解!红茶馆app辅助(透视)2025新版技巧(新2026版作弊开挂辅助插件)是一款可以让一直输...
每日必备!潮汕汇破解版(透视)... 每日必备!潮汕汇破解版(透视)教你攻略(高科技作弊开挂辅助黑科技);1、完成潮汕汇破解版的残局,帮助...
大家学习交流!钱塘十三水游戏攻... 大家学习交流!钱塘十三水游戏攻略(辅助挂)安装教程(分享作弊开挂辅助工具);人气非常高,ai更新快且...
揭秘几款!we poker免费... 揭秘几款!we poker免费辅助器(透视)AA德州教程(科技作弊开挂辅助黑科技);小薇(透视辅助)...
分享认知!微信边锋辅助(辅助挂... 分享认知!微信边锋辅助(辅助挂)2025版教程(软件作弊开挂辅助神器)是一款可以让一直输的玩家,快速...
一分钟了解!开心泉州小程序辅助... 您好,开心泉州小程序辅助哪里查看这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...