leetcode训练(C语言) 036~040

36. 有效的数独

void clean(int arr[10])
{
    memset(arr, 0, sizeof(int) * 10);
}

bool isValidSudoku(char** board, int boardSize, int* boardColSize)
{
    int arr1[10];
    int arr2[10];
    int i, j;
    // 判断横竖
    for(i = 0; i < 9; i++) {
        clean(arr1);
        clean(arr2);
        for(j = 0; j < 9; j++) {
            if(board[i][j] >= '0' && board[i][j] <= '9') {
                if(arr1[board[i][j] - '0']) {
                    return false;
                }
                arr1[board[i][j] - '0'] = 1;
            }
            if(board[j][i] >= '0' && board[j][i] <= '9') {
                if(arr2[board[j][i] - '0']) {
                    return false;
                }
                arr2[board[j][i] - '0'] = 1;                
            }
        }
    }   
    // 判断田字格
    int k, l;
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            clean(arr1);
            for(k = 3 * i; k < 3 * i + 3; k++) {
                for(l = 3 * j; l < 3 * j + 3; l++){
                    if(board[k][l] >= '0' && board[k][l] <= '9') {
                        if(arr1[board[k][l] - '0']) {
                            return false;
                        }
                        arr1[board[k][l]-'0'] = 1;                        
                    }
                }
            }
        }
    }
    return true;
}

 

37. 解数独(待定)

 

38. 外观数列

char * countAndSay(int n){
    char* mark = (char*)malloc(sizeof(char) * 5000);
    char* temp = (char*)malloc(sizeof(char) * 5000);
    mark[0] = '1';
    mark[1] = '\0';
    int i, count, j;
    char key, *p;
    for(i = 1; i< n; i++) {
        j = 0;
        key = mark[0];
        count = 0;
        p = mark;
        while(*p != '\0') {
            if(*p == key) {
                count++;
            }
            else {
                temp[j++] = count + '0';
                count = 1;
                temp[j++] = key;
                key = *p;
            }
            p++;
        }
        temp[j++] = count + '0';
        temp[j++] = key;
        temp[j] = '\0';
        strcpy(mark, temp);
    }
    return mark;
}

 

39. 组合总和

/**
 * 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* candidates, int ind, int candidatesSize, int target, int nums[], int n, int** arr, int* returnSize, int** returnColumnSizes) 
{
    if (target < 0) {
        return;
    }
    if (target == 0) {
        arr[*returnSize] = (int*)malloc(sizeof(int) * n);
        for (int i = 0; i < n; i++) {
            arr[(*returnSize)][i] = nums[i];
        }
        (*returnColumnSizes)[*returnSize] = n;
        (*returnSize)++;
        return;
    }
    for (int i = ind; i < candidatesSize; i++) {
        nums[n] = candidates[i];
        dfs(candidates, i, candidatesSize, target - nums[n], nums, n + 1, arr, returnSize, returnColumnSizes);
    }
}

int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes)
{
    *returnSize = 0;
    if (candidatesSize == 0) {
        return 0;
    }
    int** arr = (int**)malloc(sizeof(int*) * 1000000);
    *returnColumnSizes = (int*)malloc(sizeof(int) * 1000000);
    int nums[target];
    dfs(candidates, 0, candidatesSize, target, nums, 0, arr, returnSize, returnColumnSizes);
    return arr;
}

 

40. 组合总和 II

/**
 * 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* a, const void* b) 
{
    return *(int*)a - *(int*)b;
}
void dfs(int* candidates, int ind, int candidatesSize, int target, int nums[], int n, int** arr, int* returnSize, int** returnColumnSizes) 
{
    if (target < 0) {
        return ;
    }
    if (target == 0) {
        arr[*returnSize] = (int*)malloc(sizeof(int) * n);
        for (int i = 0; i < n; i++) {
            arr[(*returnSize)][i] = nums[i];
        }
        (*returnColumnSizes)[*returnSize] = n;
        (*returnSize)++;
        return ;
    }
    int i = ind;
    while (i < candidatesSize) {
        nums[n] = candidates[i];
        dfs(candidates, i + 1, candidatesSize, target - nums[n], nums, n + 1, arr, returnSize, returnColumnSizes);
        while (i < candidatesSize - 1 && candidates[i] == candidates[i + 1]) {
            i++;
        }
        i++;
    }
}
int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes)
{
    *returnSize = 0;
    if (candidatesSize == 0) {
        return 0;
    }
    qsort(candidates, candidatesSize, sizeof(int), cmp);
    int** arr = (int**)malloc(sizeof(int*) * 1000000);
    (*returnColumnSizes) = (int*)malloc(sizeof(int) * 1000000);
    int nums[target];
    dfs(candidates, 0, candidatesSize, target, nums, 0, arr, returnSize, returnColumnSizes);
    return arr;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值