[LeetCode]350. Intersection of Two Arrays II ★

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/xingyu97/article/details/100520255

Title Description

. Given two arrays, write a function to compute their intersection
subject to the effect: find the intersection of two arrays of all, repeated also considered

Sample

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 Solution

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. To sort, compare, and merge an array of similar, but different final element of choice.

C language Solution

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.
题后反思:无

I think my writing is also more difficult paste, be achieved hash algorithm in C language.

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

This paper is my personal understanding, if the wrong place welcome comments below tell me, I promptly corrected, common progress

Guess you like

Origin blog.csdn.net/xingyu97/article/details/100520255