• LeetCode //C - 77. Combinations


    77. Combinations

    Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].

    You may return the answer in any order.
     

    Example 1:

    Input: n = 4, k = 2
    Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
    Explanation: There are 4 choose 2 = 6 total combinations.
    Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.

    Example 2:

    Input: n = 1, k = 1
    Output: [[1]]
    Explanation: There is 1 choose 1 = 1 total combination.

    Constraints:
    • 1 <= n <= 20
    • 1 <= k <= n

    From: LeetCode
    Link: 77. Combinations


    Solution:

    Ideas:

    1. DFS Function (dfs):

    • This function recursively constructs the combinations.
    • If the current combination size (currentSize) is equal to k, the combination is complete. We copy this combination to our results array and return.
    • If start exceeds n, it means we’ve crossed the limit of numbers available, so we return without making further decisions.
    • For each number start, we:
      • Include it in the combination and recursively explore further numbers.
      • Exclude it from the combination and recursively explore further numbers.

    2. Main combine Function:

    • We initially allocate a large buffer (initialSize) for the results array to hold the combinations. The size is set to 2 20 2^{20} 220 , which is a safe upper bound given the constraints.
    • We call the dfs function to start constructing the combinations starting from the number 1.
    • After generating all combinations, we resize the results and returnColumnSizes arrays to the actual size of the generated combinations to conserve memory.
    Code:
    /**
     * 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 dfs(int n, int k, int start, int* currentCombination, int currentSize, int*** results, int* resultSize, int** returnColumnSizes) {
        if (currentSize == k) {
            int* combination = (int*)malloc(sizeof(int) * k);
            for (int i = 0; i < k; i++) {
                combination[i] = currentCombination[i];
            }
            results[*resultSize] = combination;
            (*returnColumnSizes)[*resultSize] = k;
            (*resultSize)++;
            return;
        }
        
        if (start > n) return;
        
        // include the current number
        currentCombination[currentSize] = start;
        dfs(n, k, start + 1, currentCombination, currentSize + 1, results, resultSize, returnColumnSizes);
        
        // exclude the current number
        dfs(n, k, start + 1, currentCombination, currentSize, results, resultSize, returnColumnSizes);
    }
    
    int** combine(int n, int k, int* returnSize, int** returnColumnSizes) {
        int initialSize = 1 << 20; // 2^20
        int** results = (int**)malloc(sizeof(int*) * initialSize);
        *returnColumnSizes = (int*)malloc(sizeof(int) * initialSize);
    
        *returnSize = 0;
        int* currentCombination = (int*)malloc(sizeof(int) * k);
        dfs(n, k, 1, currentCombination, 0, results, returnSize, returnColumnSizes);
        free(currentCombination);
    
        // Resize to actual size
        results = (int**)realloc(results, sizeof(int*) * (*returnSize));
        *returnColumnSizes = (int*)realloc(*returnColumnSizes, sizeof(int) * (*returnSize));
    
        return results;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    同等参数中最强,在苹果15Pro上也能运行!谷歌又“卷”出了端侧小模型 Gemma 2 2B...
    Java之SpringCloud Alibaba【八】【Spring Cloud微服务Gateway整合sentinel限流】
    win系统环境搭建(九)——Windows安装chatGPT
    线性PEG磷脂甲氧基-聚乙二醇-双肉豆蔻磷脂酰乙醇胺 mPEG-DMPE
    本地服务访问图片列表,图片403问题解决
    matplotlib之pyplot模块之直方图(hist():基础参数,返回值)
    Oracle数据库字符集概述及修改方式
    【Django】Django Class-Based Views (CBV) 与 DRF APIView 的区别解析
    【操作系统】本地ping出现一般故障解决方案
    MAUI+Masa Blazor APP 各大商店新手发布指南(三)vivo篇
  • 原文地址:https://blog.csdn.net/navicheung/article/details/133693142