【代碼隨想錄】【算法訓練營】【第27天】 [39]組合總和 [40] 組合總和II [131]分割回文串

前言

思路及算法思維,指路 代碼隨想錄。
題目來自 LeetCode。

day26, 休息的周末~
day 27,周一,庫存沒了,哭死~

題目詳情

[39] 組合總和

題目描述

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
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 分割回文串
131 分割回文串

解題思路

前提:分割問題
思路:。
重點:。

代碼實現

C語言
// 待補充

今日收獲

  1. 組合子集問題:去重,同一樹層去重 vs 同一樹杈去重
  2. 切割問題。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/21939.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/21939.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/21939.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C# :IQueryable IEnumerable

文章目錄 1. IEnumerable2. IQueryable3. LINQ to SQL4. IEnumerable & IQueryable4.1 Expression4.2 Provider 1. IEnumerable namespace System.Collections: public interface IEnumerable {public IEnumerator GetEnumerator (); }public interface IEnumerator {pubi…

氣泡式水位計施工技術要求

1、氣泡式水位計壓力氣管出氣口應安裝并固定在最低水位處&#xff0c;其壓力氣管也應固定&#xff0c;有條件的可用金屬管或塑料管保護。氣泡式水位計安裝示意圖見附圖。 2、安裝要求 1&#xff09;檢查氣泡式水位計氣管外觀有無破損及變形&#xff1b; 2&#xff09;旋開帶有…

面試數據庫八股文十問十答第十期

面試數據庫八股文十問十答第十期 作者&#xff1a;程序員小白條&#xff0c;個人博客 相信看了本文后&#xff0c;對你的面試是有一定幫助的&#xff01;關注專欄后就能收到持續更新&#xff01; ?點贊?收藏?不迷路&#xff01;? 1&#xff09;為什么不推薦多表Join&…

特征工程技巧—Bert

前段時間在參加比賽&#xff0c;發現有一些比賽上公開的代碼&#xff0c;其中的數據預處理步驟值得我們參考。 平常我們見到的都是數據預處理&#xff0c;現在我們來講一下特征工程跟數據預處理的區別。 數據預處理是指對原始數據進行清洗、轉換、縮放等操作&#xff0c;以便為…

Blackwell未來發展之路究竟如何?

英偉達Blackwell如何重塑AI計算的未來&#xff1f; 前言 臺灣大學演講 就在6月2日&#xff0c;英偉達CEO黃仁勛在中國臺灣大學綜合體育館發表了最新的演講。這次黃仁勛的演講依舊重磅&#xff0c;更值得注意的是這次演講中還透露了Blackwell今后的發展之路。 介紹Blackwell 介紹…

MongoDB CRUD操作:地理位置查詢

MongoDB CRUD操作&#xff1a;地理位置查詢 文章目錄 MongoDB CRUD操作&#xff1a;地理位置查詢地理空間數據GeoJSON對象傳統坐標對通過數組指定&#xff08;首選&#xff09;通過嵌入文檔指定 地理空間索引2dsphere2d 地理空間查詢地理空間查詢運算符地理空間聚合階段 地理空…

拿筆記下來!產品采購制造類合同怎樣寫比較穩妥?

拿筆記下來&#xff01;產品采購制造類合同怎樣寫比較穩妥&#xff1f; 近日&#xff0c;幾經波折&#xff0c;泰中兩國終于完成了潛艇采購談判&#xff01;你知道嗎&#xff1f;產品制造類合同或協議在起草前如果沒有充分考慮各種因素&#xff0c;可能會導致一系列問題和不利…

C語言學習:數據類型

一、 為什么要引入數據類型 ? 計算機中每個字節都有一個地址&#xff08;類似門牌號&#xff09; ? CPU通過 地址 來訪問這個字節的空間 0x20001103 1 0 0 1 0 0 1 1 0x20001102 1 1 1 0 1 1 1 0 0x20001101 1 1 1 1 0 1 0 1 0x20001100 0 …

linux c socket編程里SO_REUSEADDR的作用

比如下面的代碼 int reuse 1; int ret setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse)); if (ret SOCKET_ERROR) {log_error("_SetReuseAddr failed, err_code%d, fd%d", _GetErrorCode(), fd); }代碼解釋 setsockopt 函數用于設置…

無人監控視頻輸出卡頓狀態

設計思路&#xff0c;如下&#xff1a; 1.通過采集卡將視頻信號輸出到個人PC中 2.PC按設置好的時間&#xff0c;視頻屬性分片保存 3.將步驟2中的視頻&#xff0c;按預處理要求&#xff0c;得到待計算的視頻片段 4.使用SSIM算法計算預處理后的視頻&#xff0c;將計算得到的數據存…

聊天機器人的實踐過程

一、語聊機器人 OpenAI 的爆火&#xff0c;到如今也才一年多的時間&#xff0c;然而在過去的一年中&#xff0c;生成式AI的落地場景幾乎 80%都是 ChatBot 的形式&#xff0c;那么今天這篇文章我們就來聊一下&#xff0c;生成式AI和IM能擦出怎么樣的火花&#xff1f;以及各種場…

p13idea的其他操作

1 導入模塊 錯誤示范&#xff1a; 正確示范&#xff1a; 2 刪除模塊 必須用delete才能刪除干凈&#xff0c;用remove刪了之后還要回到文件里面把它刪除掉

有錢還系統源碼 人人還眾籌還錢模式還貸系統源碼

盈利模式&#xff1a; 1.系統里直推400 2.間推得200 3.升級是隔代匹配200 4.漏單直接設置歸系統 5.九級匹配不到直接歸平臺 有錢還平臺新注冊會員&#xff0c;即新入的負債者要分9次分別資助先來的11名負債者每人200元&#xff0c;這筆資助不是一次性給到對方&#xff0c…

Prism 入門04,導航功能

當前章節,沿用 上一章使用Prism 框架創建的WPF 項目空模板。在上一章節,各個不同的模塊之間能夠進行切換并把內容呈現在主程序的頁面當中(其實是通過在主程序中注冊的區域去發起一個導航的請求,然后跳轉到對應的視圖。也就是實現了導航跳轉功能)。 為什么能實現導航的跳轉?…

Mybatis的一級緩存

緩存 MyBatis 包含一個非常強大的查詢緩存特性,它可以非常方便地配置和定制。MyBatis 3 中的緩存實現的很多改進都已經實現了,使得它更加強大而且易于配置。 Mybatis和Hibernate一樣&#xff0c;也有一級和二級緩存&#xff0c;同樣默認開啟的只有一級緩存&#xff0c;二級緩…

docker-compose安裝多環境apollo

下載數據庫sql文件 https://github.com/apolloconfig/apollo/blob/master/scripts/sql/src/apolloconfigdb.sql https://github.com/apolloconfig/apollo/blob/master/scripts/sql/src/apolloportaldb.sql 創建庫并導入表 #生產環境 mysql> CREATE DATABASE IF NOT EXIS…

腦部磁共振成像腫瘤分割方法(MATLAB 2018)

近年腦腫瘤發病率呈上升趨勢&#xff0c;約占全身腫瘤的5%&#xff0c;占兒童腫瘤的70%。CT、MRI等多種影像檢查方法可用于檢測腦腫瘤&#xff0c;其中MRI應用于腦腫瘤成像效果最佳。精準的腦腫瘤分割是病情診斷、手術規劃及后期治療的必備條件&#xff0c;既往研究者對腦部腫瘤…

Python知識點12---Python的I/O操作

提前說一點&#xff1a;如果你是專注于Python開發&#xff0c;那么本系列知識點只是帶你入個門再詳細的開發點就要去看其他資料了&#xff0c;而如果你和作者一樣只是操作其他技術的Python API那就足夠了。 Python的流(I/O)操作&#xff0c;最簡單的其實就是輸入和輸出&#x…

擴展翡蜀定理問題

問題描述 給定一個大小為 n n n 的集合 A { a 1 , a 2 ~ a n } A\{a_1,a_2 \sim a_n\} A{a1?,a2?~an?}&#xff0c;滿足條件 gcd ( A ) 1 \text{gcd}(A)1 gcd(A)1。 O ( 1 ) O(1) O(1)時間內 求最大的 k k k &#xff0c;滿足不存在一個大小為 n n n 的非負數集合…

工廠的精益生產如此重要

什么是工廠的精益生產 精益生產&#xff08;Lean Manufacturing&#xff09;是一種起源于20世紀50年代日本豐田汽車公司的生產管理哲學。它的核心理念是通過消除生產過程中的浪費&#xff0c;優化流程&#xff0c;提高效率&#xff0c;從而實現成本降低和質量提升。精益生產不僅…