自己的网站如何做分销,怎么做水果网站,wordpress 首页展示,专业做网站排名多少钱前言
思路及算法思维#xff0c;指路 代码随想录。 题目来自 LeetCode。
day26#xff0c; 休息的周末~ day 27#xff0c;周一#xff0c;库存没了#xff0c;哭死~
题目详情
[39] 组合总和
题目描述
39 组合总和
解题思路
前提#xff1a;组合的子集问题…前言
思路及算法思维指路 代码随想录。 题目来自 LeetCode。
day26 休息的周末~ day 27周一库存没了哭死~
题目详情
[39] 组合总和
题目描述
39 组合总和
解题思路
前提组合的子集问题统一元素可以重复选取 思路回溯 剪枝。 重点剪枝的前提是数组已排序。
代码实现
C语言
回溯 未排序剪枝
/*** Return an array of arrays of size *returnSize.* The sizes of the arrays are returned as *returnColumnSizes array.* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/void backtracing(int* candidates, int candidatesSize, int target, int index, int *nums, int numsSize, int ***ans, int* returnSize, int** returnColumnSizes)
{// 退出条件if (0 target){*ans (int **)realloc(*ans, sizeof(int *) * ((*returnSize) 1));(*ans)[*returnSize] (int *)malloc(sizeof(int) * (numsSize));for (int i 0; i numsSize; i){(*ans)[*returnSize][i] nums[i];}*returnColumnSizes (int *)realloc(*returnColumnSizes, sizeof(int) * ((*returnSize) 1));(*returnColumnSizes)[*returnSize] numsSize;(*returnSize);return ;}for (int j index; j candidatesSize; j){if (target candidates[j]){continue ;}// 递归nums[numsSize] candidates[j];numsSize;backtracing(candidates, candidatesSize, target - candidates[j], j, nums, numsSize, ans, returnSize, returnColumnSizes);// 回溯numsSize--;nums[numsSize] 0;}return ;
}int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {// 判空if (candidatesSize 0){return NULL;}// 输出int **ans NULL;int nums[41];int index 0;*returnSize 0;printf(%d\n, target);backtracing(candidates, candidatesSize, target, 0, nums, 0, ans, returnSize, returnColumnSizes);if (*returnSize 0){return NULL;}return ans;
}回溯 排序 剪枝
/*** Return an array of arrays of size *returnSize.* The sizes of the arrays are returned as *returnColumnSizes array.* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/int cmp(const void *p1, const void *p2)
{return *(int *)p1 *(int *)p2;
}void backtracing(int* candidates, int candidatesSize, int target, int index, int *nums, int numsSize, int ***ans, int* returnSize, int** returnColumnSizes)
{// 退出条件if (0 target){*ans (int **)realloc(*ans, sizeof(int *) * ((*returnSize) 1));(*ans)[*returnSize] (int *)malloc(sizeof(int) * (numsSize));for (int i 0; i numsSize; i){(*ans)[*returnSize][i] nums[i];}*returnColumnSizes (int *)realloc(*returnColumnSizes, sizeof(int) * ((*returnSize) 1));(*returnColumnSizes)[*returnSize] numsSize;(*returnSize);return ;}// 剪枝for (int j index; (j candidatesSize) (target candidates[j]); j){// 递归nums[numsSize] candidates[j];numsSize;backtracing(candidates, candidatesSize, target - candidates[j], j, nums, numsSize, ans, returnSize, returnColumnSizes);// 回溯numsSize--;nums[numsSize] 0;}return ;
}int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {// 判空if (candidatesSize 0){return NULL;}// 排序qsort(candidates, candidatesSize, sizeof(int), cmp);// 输出int **ans NULL;int nums[41];int index 0;*returnSize 0;backtracing(candidates, candidatesSize, target, 0, nums, 0, ans, returnSize, returnColumnSizes);if (*returnSize 0){return NULL;}return ans;
}[40] 组合总和II
题目描述
40 组合总和II
解题思路
前提组合的子集问题同一元素只能使用一次但是结果不包含重复组合 思路回溯 剪枝 重点结果子集中排除重复组合需要树形结构中同一树层的相同的元素值不可重复选取使用used数组实现去重。
代码实现
C语言
利用used数组 false同一树层 去重
/*** Return an array of arrays of size *returnSize.* The sizes of the arrays are returned as *returnColumnSizes array.* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/int cmp(const void *p1, const void *p2)
{return *(int *)p1 *(int *)p2;
}void backtracing(int* candidates, int candidatesSize, int target, int index, int *nums, int numsSize, bool *used, int ***ans, int* returnSize, int** returnColumnSizes)
{// 退出条件if (0 target){*ans (int **)realloc(*ans, sizeof(int *) * ((*returnSize) 1));(*ans)[*returnSize] (int *)malloc(sizeof(int) * (numsSize));for (int i 0; i numsSize; i){(*ans)[*returnSize][i] nums[i];}*returnColumnSizes (int *)realloc(*returnColumnSizes, sizeof(int) * ((*returnSize) 1));(*returnColumnSizes)[*returnSize] numsSize;(*returnSize);return ;}for (int j index; (j candidatesSize) (target candidates[j]); j){// 去重if ((j 0) (candidates[j] candidates[j - 1]) (used[j - 1] false)){continue;}// 递归nums[numsSize] candidates[j];used[j] true;numsSize;backtracing(candidates, candidatesSize, target - candidates[j], j 1, nums, numsSize, used, ans, returnSize, returnColumnSizes);// 回溯numsSize--;used[j] false;nums[numsSize] 0;}return ;
}int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {// 判空if (candidatesSize 0){return NULL;}// 排序qsort(candidates, candidatesSize, sizeof(int), cmp);// 输出int **ans NULL;int nums[100] {0};bool used[100] {false};int index 0;*returnSize 0;backtracing(candidates, candidatesSize, target, 0, nums, 0, used, ans, returnSize, returnColumnSizes);if (*returnSize 0){return NULL;}return ans;
}[131] 分割回文串
题目描述
131 分割回文串
解题思路
前提分割问题 思路。 重点。
代码实现
C语言
// 待补充今日收获
组合子集问题去重同一树层去重 vs 同一树杈去重切割问题。 文章转载自: http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.lyldhg.cn.gov.cn.lyldhg.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.hjrjr.cn.gov.cn.hjrjr.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.qxdrw.cn.gov.cn.qxdrw.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.ydzly.cn.gov.cn.ydzly.cn http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.knswz.cn.gov.cn.knswz.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.ydnxm.cn.gov.cn.ydnxm.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.plydc.cn.gov.cn.plydc.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.lbcfj.cn.gov.cn.lbcfj.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.4r5w91.cn.gov.cn.4r5w91.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.trjr.cn.gov.cn.trjr.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.wypyl.cn.gov.cn.wypyl.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.tygn.cn.gov.cn.tygn.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.nyzmm.cn.gov.cn.nyzmm.cn http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn