代码随想录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;       } };  

相关内容

热门资讯

辅助透视!aapoker辅助怎... 辅助透视!aapoker辅助怎么用,微信小程序微乐辅助器免费版v2.0免费,窍门教程(有挂方法)-哔...
黑科技技巧!来玩app破解版,... 黑科技技巧!来玩app破解版,杭州边锋网络游戏辅助,烘培教程(有挂实锤)-哔哩哔哩在进入杭州边锋网络...
据了解!中至窝龙辅助,雀友会广... 据了解!中至窝龙辅助,雀友会广东潮汕辅助软件,项目教程(有人有挂)-哔哩哔哩一、雀友会广东潮汕辅助软...
透视软件!agpoker辅助,... 透视软件!agpoker辅助,微信小程序微乐辅助器,绝活教程(有挂存在)-哔哩哔哩1、透视软件!ag...
据了解!娱乐辅助系统,拼十辅助... 据了解!娱乐辅助系统,拼十辅助器,教材教程(有挂透明挂)-哔哩哔哩1、娱乐辅助系统免费辅助多个强度级...
分享透视!aa poker辅助... 分享透视!aa poker辅助,微乐小程序插件免费贴吧,项目教程(有挂分享)-哔哩哔哩所有人都在同一...
透视科技!cloudpoker... 透视科技!cloudpoker外开挂,情怀宜春麻辣自建房辅助app,指引教程(有挂方式)-哔哩哔哩进...
据通报!新西部辅助外卦,欢乐茶... 据通报!新西部辅助外卦,欢乐茶馆修改器,窍门教程(确实有挂)-哔哩哔哩;1、不需要AI权限,帮助你快...
揭露透视!wepoker私人局... 揭露透视!wepoker私人局外卦,微乐游戏兑换码大全,窍要教程(有挂透视)-哔哩哔哩1、全新机制【...
透视教学!steampoker... 透视教学!steampokermaster辅助,全来潜山跑风破解版安卓,学习教程(有挂讲解)-哔哩哔...