threeal / leetspace

A dedicated workspace and archive for my LeetCode submissions
https://leetcode.com/threeal
MIT License
1 stars 1 forks source link

Solve Problem 78. Subsets in C Language #957

Open threeal opened 1 month ago

threeal commented 1 month ago

This issue suggests solving the problem 78. Subsets. To achieve this, one can simply follow the same solution as the C++ solution of this problem in #924.

Rejwan321 commented 1 month ago

int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    *returnSize = 1 << numsSize;
    int** result = (int**)malloc(*returnSize * sizeof(int*));
    *returnColumnSizes = (int*)malloc(*returnSize * sizeof(int));
    for (int i = 0; i < *returnSize; i++) {
        int subsetSize = 0;
        int temp = i;
        while (temp > 0) {
            subsetSize += temp & 1;
            temp >>= 1;
        }
        result[i] = (int*)malloc(subsetSize * sizeof(int));
        (*returnColumnSizes)[i] = subsetSize;
        int index = 0;
        for (int j = 0; j < numsSize; j++) {
            if (i & (1 << j)) {
                result[i][index++] = nums[j];
            }
        }
    }
    return result;
}
Rejwan321 commented 1 month ago
  1. Subsets solved using C language