[LeetCode]350. Intersection of Two Arrays II ★

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/xingyu97/article/details/100520255

题目描述

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;
}

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

猜你喜欢

转载自blog.csdn.net/xingyu97/article/details/100520255