算法 LC 两个数组的交集 II

题目描述

LeetCode - 题350

给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。可以不考虑输出结果的顺序。

示例 1:
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2,2]

示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[4,9]

题解

思路1:哈希表

使用哈希表记录其中一个数组中每个元素出现的次数。然后遍历另一个数组,对于第二个数组中的每个数字,如果哈希表中存在该元素,则将该数字添加到答案,并减少哈希表中该数字出现的次数

为了降低空间复杂度,首先遍历较短的数组并在哈希表中记录每个数字以及对应出现的次数,然后遍历较长的数组得到交集。

// OC
+ (NSArray *)intersect1:(NSArray *)nums1 nums2:(NSArray *)nums2 {
    if (nums1.count > nums2.count) {
        return [self intersect1:nums2 nums2:nums1];
    }
    NSMutableArray *resArray = [[NSMutableArray alloc] init];
    NSMutableDictionary *tableDict = [[NSMutableDictionary alloc] init];
    for (int i=0; i<nums1.count; i++) {
        NSString *tempkey = [NSString stringWithFormat:@"%@",nums1[i]];
        if (tableDict[tempkey] == nil) {
            tableDict[tempkey] = @1;
        }else{
            tableDict[tempkey] = [NSNumber numberWithInt:[tableDict[tempkey] intValue] + 1];
        }
    }
    
    for (int j=0; j<nums2.count; j++) {
        NSString *tempkey = [NSString stringWithFormat:@"%@",nums2[j]];
        if ([tableDict[tempkey] intValue] > 0) {
            tableDict[tempkey] = [NSNumber numberWithInt:[tableDict[tempkey] intValue] - 1];
            [resArray addObject:nums2[j]];
            
        }
    }
    return resArray;
    
}
// Swift
    static public func intersect1(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
        if nums1.count > nums2.count {return intersect1(nums2, nums1)}
        var resArray = [Int]()
        var tableDict = [Int:Int]()
        
        for i in 0..<nums1.count {
            tableDict[nums1[i]] = (tableDict[nums1[i]] ?? 0) + 1
        }
        for i in 0..<nums2.count {
            if (tableDict[nums2[i]] ?? 0) > 0 {
                tableDict[nums2[i]] = (tableDict[nums2[i]] ?? 0) - 1
                resArray.append(nums2[i])
            }
        }
        return resArray;
    }

思路2:排序+双指针

如果两个数组是有序的,则可以使用双指针的方法得到两个数组的交集。

首先对两个数组进行排序,然后使用两个指针遍历两个数组。

初始时,两个指针分别指向两个数组的头部。每次比较两个指针指向的两个数组中的数字,如果两个数字不相等,则将指向较小数字的指针右移一位,如果两个数字相等,将该数字添加到答案,并将两个指针都右移一位。当至少有一个指针超出数组范围时,遍历结束

// OC
+ (NSArray *)intersect2:(NSArray *)nums1 nums2:(NSArray *)nums2 {
    NSMutableArray *resArray = [[NSMutableArray alloc] init];
    
    NSArray *sortnums1 = [nums1 sortedArrayUsingSelector:@selector(compare:)];
    NSArray *sortnums2 = [nums2 sortedArrayUsingSelector:@selector(compare:)];
    
    
    int first = 0;
    int second = 0;
    while (first < sortnums1.count && second < sortnums2.count) {
        if ([sortnums1[first] intValue] == [sortnums2[second] intValue]) {
            [resArray addObject:sortnums1[first]];
            first ++;
            second ++;
        }else if ([sortnums1[first] intValue] > [sortnums2[second] intValue]){
            second ++;
        }else{
            first ++;
        }
        
    }
    return resArray;
    
}
// Swift
    static public func intersect2(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
        
        var resArray = [Int]()
        let nums1 = nums1.sorted()
        let nums2 = nums2.sorted()
        
        var first = 0
        var second = 0
        
        while (first < nums1.count && second < nums2.count) {
            if nums1[first] == nums2[second] {
                resArray.append(nums1[first])
                first += 1
                second += 1
            }else if nums1[first] > nums2[second] {
                second += 1
            }else{
                first += 1
            }
        }
        return resArray;
    }

参考:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容