[LeetCode]350. Intersection of Two Arrays II ★

题目描述

Given two arrays, write a function to compute their intersection.
题目大意:求两个数组的全部交集,重复的也算

样例

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

python解法

class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        nums1.sort()
        nums2.sort()
        i,j = 0, 0
        result = []
        while i<len(nums1)and j<len(nums2):
            if nums1[i] == nums2[j]:
                result.append(nums1[i])
                i += 1
                j += 1
            elif nums1[i] < nums2[j]:
                i += 1
            else:
                j += 1
        return result

Runtime: 60 ms, faster than 43.42% of Python3 online submissions for Intersection of Two Arrays II.
Memory Usage: 13.6 MB, less than 5.72% of Python3 online submissions for Intersection of Two Arrays II.
题后反思:

  1. 先排序,再比较,和数组合并类似,只不过最终元素取舍不同。

C语言解法

int cmp(const void*a, const void *b)
{
    return *(int*)a > *(int*)b ? 1:-1;
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    qsort(nums1, nums1Size, sizeof(int), cmp);
    qsort(nums2, nums2Size, sizeof(int), cmp);
    int i = 0,j = 0;
    int *returnValue = NULL;
    *returnSize = 0;
    while(i<nums1Size && j<nums2Size)
    {
        if (nums1[i] == nums2[j])
        {
            returnValue = (int*)realloc(returnValue, sizeof(int)*(*returnSize+1));
            returnValue[*returnSize] = nums1[i];
            (*returnSize)++;
            i++;
            j++;
        }
        else if (nums1[i] < nums2[j])
            i++;
        else 
            j++;
    }
    return returnValue;
}

Runtime: 4 ms, faster than 97.89% of C online submissions for Intersection of Two Arrays II.
Memory Usage: 9 MB, less than 100.00% of C online submissions for Intersection of Two Arrays II.
题后反思:无

我的我觉得难度较大的写法也贴上,算是用C语言实现哈希算法。

struct linklist{
    int val;
    struct linklist *next;
};
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    if (!nums1Size || !nums2Size)
    {
        *returnSize=0;
        return NULL;
    }
    if (nums1Size < nums2Size)
    {
        int t = nums1Size;
        nums1Size = nums2Size;
        nums2Size = t;
        int *x = nums1;
        nums1 = nums2;
        nums2 = x;
    }
    
    struct linklist *num[nums2Size];
    for(int i=0;i<nums2Size;i++)
        num[i] = NULL;
    int remain = 0;
    struct linklist *link = NULL;
    for(int i=0;i<nums2Size;i++)
    {
        remain = (nums2[i]%nums2Size+nums2Size)%nums2Size;
        link = (struct linklist *)malloc(sizeof(struct linklist));
        link -> val = nums2[i];
        link -> next = num[remain];
        num[remain] = link;
    }
    int *returnValue = NULL;
    *returnSize = 0;
    for (int i=0;i<nums1Size;i++)
    {
        remain = (nums1[i]%nums2Size+nums2Size)%nums2Size;
        link = num[remain];
        while(link)
        {
            if(link -> val == nums1[i])
            {
                returnValue = (int*)realloc(returnValue, sizeof(int)*(*returnSize + 1));
                returnValue[*returnSize] = nums1[i];
                (*returnSize) ++;
                if(link -> next)
                {
                    link -> val = link -> next ->val;
                    link -> next = link -> next -> next;
                }
                else if (link == num[remain])
                {
                    num[remain] = NULL;
                }
                else
                {
                    struct linklist *p = num[remain];
                    while(p->next != link)
                        p = p -> next;
                    p -> next = link -> next;
                }
                break;
            }
            link = link->next;
        }
    }
    return returnValue;
}

文中都是我个人的理解,如有错误的地方欢迎下方评论告诉我,我及时更正,大家共同进步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值