學渣帶你刷Leetcode0039組合總和

題目描述

給定一個無重複元素的數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和爲 target 的組合。

candidates 中的數字可以無限制重複被選取。

說明:

所有數字(包括 target)都是正整數。
解集不能包含重複的組合。 
示例 1:

輸入: candidates = [2,3,6,7], target = 7,
所求解集爲:
[
  [7],
  [2,2,3]
]
示例 2:

輸入: candidates = [2,3,5], target = 8,
所求解集爲:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/combination-sum
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

白話題目:
 

算法:

 

詳細解釋關注 B站  【C語言全代碼】學渣帶你刷Leetcode 不走丟 https://www.bilibili.com/video/BV1C7411y7gB

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().
 */
/**
 * 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().
 */
/* 定義返回二維數組的行數 */
#define MAX_SIZE 600
/* 標明輸入、輸出參數 */
#define PARAM_IN 
#define PARAM_OUT 

static void dfs(PARAM_IN int* candidates, PARAM_IN int candidatesSize, PARAM_IN int target,
                PARAM_OUT int* returnSize, PARAM_OUT int** returnColumnSizes, 
                PARAM_OUT int** ppRes, PARAM_IN int* pBuffer, PARAM_IN int indexOfCan, PARAM_IN int indexOfBuf)
{
    int index = 0;

    if (0 == target)
    {  
        // 存入新值,更新返回數組
        ppRes[*returnSize] = (int*)malloc(indexOfBuf * sizeof(int));
        memcpy(ppRes[*returnSize], pBuffer, indexOfBuf * sizeof(int));
        (*returnColumnSizes)[*returnSize] = indexOfBuf;
        (*returnSize)++;
    }
    else if (0 < target)
    {
        for (index = indexOfCan; index <= candidatesSize - 1; index++)
        {
            if (indexOfCan != index && candidates[index - 1] == candidates[index]) // 若index爲indexOfCan,必執行一次,類似do while
            {
                continue;
            }

            pBuffer[indexOfBuf] = candidates[index]; // 更新緩存

            dfs(candidates, candidatesSize, target - candidates[index], // 更新target
                 returnSize, returnColumnSizes,                               
                 ppRes, pBuffer, index, indexOfBuf + 1); // index作爲indexOfCan傳入,indexOfBuf加1
            // 退出後indexOfBuf回到原先值
        }
    }
}

int** combinationSum(PARAM_IN int* candidates, PARAM_IN int candidatesSize, PARAM_IN int target, 
                     PARAM_OUT int* returnSize, PARAM_OUT int** returnColumnSizes)
{
    int indexOfCan = 0;
    int indexOfBuf = 0;

    int** ppRes = (int**)malloc(MAX_SIZE * sizeof(int*)); // 返回數組
    int* pBuffer = (int*)malloc(target * sizeof(int)); // 緩存

    *returnSize = 0;
    *returnColumnSizes = (int*)malloc(MAX_SIZE * sizeof(int)); // 初始化輸出參數

    dfs(candidates, candidatesSize, target,    
        returnSize, returnColumnSizes,         
        ppRes, pBuffer, indexOfCan, indexOfBuf);
    
    return ppRes;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章