代码随想录算法训练营第七天| 454.四数相加II 、 383. 赎金信 、15. 三数之和 、18. 四数之和

题目链接:454.四数相加

这道题目刚看到的时候只有一个思路-暴力求解,但看到Carl老哥给出的提示后,才恍然大悟,想到用哈希表解决这个问题,对于哈希相关的内容,可以直接调用uthash.h库,但自己对于这个库的使用还是不熟练,看着库的使用方法,才能完成本题的书写。思路是通过两层for循环将其中两个数组元素相加所有可能求和,插入哈希表中,接着再利用两层for循环将剩余两个元素的求和,利用target减去求和,在哈希表中查找差值。下面直接给出求解代码:

struct hashTable {
    int key;
    int value;
    UT_hash_handle hh;
};

struct hashTable *hashTable;

int fourSumCount(int* nums1, int nums1Size, int* nums2, int nums2Size, int* nums3, int nums3Size, int* nums4, int nums4Size){
    hashTable = NULL;
    for (int i = 0; i < nums1Size; ++i) {
        for (int j = 0; j < nums1Size; ++j) {
            int key = nums1[i] + nums2[j];
            struct hashTable *tmp = NULL;
            HASH_FIND_INT(hashTable, &key, tmp);
            if (tmp == NULL) {
                tmp = (struct hashTable *)malloc(sizeof(struct hashTable));
                tmp->key = key;
                tmp->value = 1;
                HASH_ADD_INT(hashTable, key, tmp);
            } else {
                tmp->value++;
            }
        }
    }
    int ret = 0;
    for (int i = 0; i < nums1Size; ++i) {
        for (int j = 0; j < nums1Size; ++j) {
            int key = 0 - nums3[i] - nums4[j];
            struct hashTable *tmp = NULL;
            HASH_FIND_INT(hashTable, &key, tmp);
            if (tmp != NULL) {
                ret += tmp->value;
            }
        }
    }
    return ret;
}

时间复杂度O(N^2)
空间复杂度O(N^2)

题目链接:383.赎金信

因为元素个数只有26个,所以我们可以用哈希数组来解决这个问题,但为了锻炼自己手撕代码的能力,我利用求余法作为哈希函数解决这个问题。下面直接给出代码:

#define N 31

struct hashNode{
    int key;
    struct hashNode *next;
};

struct hashTable {
    struct hashNode hashNode[N];
};

struct hashTable *hashTable_create(void) {
    struct hashTable *hashTable;
    hashTable = (struct hashTable *)malloc(sizeof(struct hashTable));
    memset(hashTable, 0, sizeof(hashTable));
    for (int i = 0; i < N; ++i) {
        hashTable->hashNode[i].next = NULL;
    }
    return hashTable;
}

void hashTable_insert(struct hashTable *hashTable, int key) {
    int index = key % N;
    struct hashNode *node = (struct hashNode *)malloc(sizeof(struct hashNode));
    node->key = key;
    node->next = NULL;
    if ((hashTable->hashNode[index].next) == NULL) {
        hashTable->hashNode[index].next = node;
    } else {
        struct hashNode *tmp = hashTable->hashNode[index].next;
        node->next = tmp;
        hashTable->hashNode[index].next = node;
    }
}

int hashTable_find(struct hashTable *hashTable, int key) {
    int index = key % N;
    if (hashTable->hashNode[index].next == NULL) {
        return 0;
    } else {
        struct hashNode *tmp = (hashTable->hashNode)[index].next;
        hashTable->hashNode[index].next = hashTable->hashNode[index].next->next;
        free(tmp);
        tmp = NULL;
        return 1;
    }
}

bool canConstruct(char * ransomNote, char * magazine){
    if (ransomNote == NULL || magazine == NULL) {
        return false;
    }
    struct hashTable *hashTable = hashTable_create();
    int i = strlen(magazine);
    for (int j = 0; j < i; ++j) {
        hashTable_insert(hashTable, (int)(magazine[j] - 'a'));
    }
    i = strlen(ransomNote);
    for (int j = 0; j < i; ++j) {
        if (hashTable_find(hashTable , (int)(ransomNote[j] - 'a')) == 0) {
            return false;
        }
    }
    return true;
}

时间复杂度O(N)
空间复杂度O(1)

题目链接:15. 三数之和 18. 四数之和

这两道题思路一致,拿三数之和说明。毫无疑问,看到题目第一反应暴力求解,但暴力求解也存在问题,因为我们要进行去重操作,所以暴力求解也不是一个好的方法。接着就陷入了沉思。。。不知如何求解,无奈之下参考可代码随想录的方法-利用排序➕双指针的方法。

简而言之,首先利用排序算法将给的数组进行排序,这样一下重复元素彼此相邻,接着定义一个变量i作为整体循环条件,从0一直遍历到numsSize-3(因为三个数之和)。接着定义指针p和q,p指向i得下一个元素,q指向数组末尾numsSize-1,然后不断移动p和q知道p=q时,此时代码一次循环结束。注意在每一次循环的时候,我们都要对p和q所指的元素进行去重操作,具体细节详见代码随想录。下面给出三数之和的代码:

/**
 * 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 partion(int *nums, int left, int right) {
    int tmp = nums[left];
    while (left < right) {
        while (left < right && tmp <= nums[right]) {
            right--;
        }
        nums[left] = nums[right];
        while (left < right && tmp >= nums[left]) {
            left++;
        }
        nums[right] = nums[left];

    }
    nums[left] = tmp;
    return left;
}

void quick_sort(int *nums, int left, int right) {
    if (left >= right) {
        return;
    }
    int t = partion(nums, left, right);
    quick_sort(nums, left, t - 1);
    quick_sort(nums, t + 1, right);
}


int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    *returnSize = 0;
    int **data = (int **)malloc(sizeof(int *) * 18000);
    int index = 0;
    if (numsSize < 3) {
        return NULL;
    }   
    quick_sort(nums, 0, numsSize - 1);

    int left, middle, right;
    for (left = 0; left < numsSize - 2; ++left) {
        if (nums[left] > 0)
            break;
        if ((left > 0) && (nums[left] == nums[left - 1]))
            continue;
        middle = left + 1;
        right = numsSize - 1;
        while (middle < right) {
            if (nums[left] + nums[middle] + nums[right] > 0) {
                right--;
            } else if (nums[left] + nums[middle] + nums[right] < 0) {
                middle++;
            } else {
                int *tmp = (int *)malloc(sizeof(int) * 3);
                tmp[0] = nums[left];
                tmp[1] = nums[middle];
                tmp[2] = nums[right];
                data[index++] = tmp;
                while (middle < right && nums[right] == nums[right - 1]) {
                    right--;
                }
                while (middle < right && nums[middle] == nums[middle + 1]) {
                    middle++;
                }
                middle++;
                right--;
            }
        }
    }
    *returnSize = index;
    *returnColumnSizes = (int *)malloc(sizeof(int) * index);
    for (int i = 0; i < index; ++i) {
        (*returnColumnSizes)[i] = 3;
    }
    return data;
}

时间复杂度O(N^2)
空间复杂度O(1)

今天是比较艰难的一天,第一题就磕磕绊绊的,虽然过程艰难,但结果还是好的,提交成功的时候长舒一口气,不枉花费的时间,希望自己能够真正理解解题思路,继续加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值