首页 > 其他分享 >LeetCode //C - 350. Intersection of Two Arrays II

LeetCode //C - 350. Intersection of Two Arrays II

时间:2024-09-08 16:49:25浏览次数:3  
标签:Arrays ++ Two number II int result nums1 nums2

350. Intersection of Two Arrays II

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.
 

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]
Explanation: [9,4] is also accepted.

Constraints:
  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

From: LeetCode
Link: 350. Intersection of Two Arrays II


Solution:

Ideas:

1. Counting Frequencies: We first create two arrays (count1 and count2) of size 1001, initialized to 0. These arrays will store the frequency of each number from nums1 and nums2, respectively.
2. Finding the Intersection: For each number from 0 to 1000, we calculate the minimum count from both arrays. This minimum count gives the number of times this number will appear in the result.
3. Storing the Result: We store the intersection in a dynamically allocated array, keeping track of the number of elements added with the index variable.
4. Returning the Result: Finally, we set the value of returnSize to the number of elements in the result and return the result array.

Code:
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
    int count1[1001] = {0};
    int count2[1001] = {0};
    
    // Count occurrences of each element in nums1
    for (int i = 0; i < nums1Size; i++) {
        count1[nums1[i]]++;
    }
    
    // Count occurrences of each element in nums2
    for (int i = 0; i < nums2Size; i++) {
        count2[nums2[i]]++;
    }
    
    // Find the intersection
    int* result = (int*)malloc(sizeof(int) * (nums1Size < nums2Size ? nums1Size : nums2Size));
    int index = 0;
    
    for (int i = 0; i <= 1000; i++) {
        int minCount = (count1[i] < count2[i]) ? count1[i] : count2[i];
        while (minCount > 0) {
            result[index++] = i;
            minCount--;
        }
    }
    
    *returnSize = index;
    return result;
}

标签:Arrays,++,Two,number,II,int,result,nums1,nums2
From: https://blog.csdn.net/navicheung/article/details/142029400

相关文章

  • Java毕设项目II基于Spring Boot+Vue的失物招领平台
    目录一、前言二、技术介绍三、系统实现四、论文参考五、核心代码六、源码获取全栈码农以及毕业设计实战开发,CSDN平台Java领域新星创作者,专注于大学生项目实战开发、讲解和毕业答疑辅导。获取源码联系方式请查看文末一、前言在快节奏的生活中,物品遗失与寻回成为了人......
  • JDBC流ASCII和二进制数据
    PreparedStatement对象可以使用输入和输出流来提供参数数据。能够将整个文件放入可以容纳大值的数据库列,例如CLOB和BLOB数据类型。有以下方法可用于流式传输数据-setAsciiStream():此方法用于提供大的ASCII值。setCharacterStream():此方法用于提供较大的UNICODE值。setBinary......
  • Network Policy使用场景
    Kubernetes中的NetworkPolicy是一种用于控制Pod之间网络通信的机制。它允许用户定义哪些Pod可以相互通信,从而提高集群的安全性和管理性。以下是一些常见的NetworkPolicy使用场景:1.微服务架构中的流量控制在微服务架构中,不同的服务可能需要限制对彼此的访问。通过NetworkPoli......
  • LeetCode:3177. 求出最长好子序列 II 哈希表+动态规划实现N*K时间复杂度
    3177.求出最长好子序列II题目链接题目描述给你一个整数数组nums和一个非负整数k。如果一个整数序列seq满足在下标范围[0,seq.length-2]中最多只有k个下标i满足seq[i]!=seq[i+1],那么我们称这个整数序列为好序列。请你返回nums中好子序列的最长长度......
  • k8s中Network Policy的实现原理
    Kubernetes中的NetworkPolicy是一种用于控制Pod之间网络流量的机制,主要用于增强安全性和隔离性。其实现原理可以从以下几个方面进行理解:1.定义和目标NetworkPolicy定义了一组规则,这些规则决定了哪些Pod可以与其他Pod进行通信。其主要目标是:限制Pod之间的流量。增强服务的......
  • 【Golang】LeetCode面试经典150题:45. 跳跃游戏 II
    题干:给定一个长度为 n 的 0索引整数数组 nums。初始位置为 nums[0]。每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i+j] 处:0<=j<=nums[i] i+j<n返回到达 nums[n-1] 的最小跳跃次数......
  • 【Ynoi 2019 模拟赛】Yuno loves sqrt technology III
    LuoguP5048YunolovessqrttechnologyIII题意给定一个长度为\(n\)的序列\(a\)。有\(m\)次询问:查询区间\([l,r]\)中众数的出现次数。强制在线。数据范围与约定\(1\len,m,a_i\le5*10^5\)。题解十年前《蒲公英》的做法,这道题只能拿\(80\)分,因为这道题卡了空......
  • 代码随想录刷题day25丨491.递增子序列 ,46.全排列 ,47.全排列 II
    代码随想录刷题day25丨491.递增子序列,46.全排列,47.全排列II1.题目1.1递增子序列题目链接:491.非递减子序列-力扣(LeetCode)视频讲解:回溯算法精讲,树层去重与树枝去重|LeetCode:491.递增子序列_哔哩哔哩_bilibili文档讲解:https://programmercarl.com/0491.%E9%80......
  • 触想全新Z系列工控机扩展IIoT应用潜能
    8月31日,触想重磅推出全新Z系列高性能、扩展型工控机——TPC05/06/07-WIPC,提供标准版/双卡槽/四卡槽3款机型选择。作为边缘计算、机器视觉、AI智能和工业应用的理想机型,Z系列工控机支持Intel®第12/13/14代Core™i3/i5/i7/i9处理器,最多搭载4个PCIe/PCI的扩展能力,可外接多种......
  • 触想全新Z系列工控机扩展IIoT应用潜能
    8月31日,触想重磅推出全新Z系列高性能、扩展型工控机——TPC05/06/07-WIPC,提供标准版/双卡槽/四卡槽3款机型选择。作为边缘计算、机器视觉、AI智能和工业应用的理想机型,Z系列工控机支持Intel®第12/13/14代Core™i3/i5/i7/i9处理器,最多搭载4个PCIe/PCI的扩展能力,可外接多......