350.intersection-of-two-arrays-ii

本文介绍了一种计算两个数组交集的方法,包括对输入数组进行排序并使用双指针技术来找出共同元素。该方法适用于已排序数组,并讨论了不同情况下的优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定两个数组,写一个方法来计算它们的交集。

例如:
给定 nums1 = [1, 2, 2, 1]nums2 = [2, 2], 返回 [2, 2].

注意:

  •    输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。
  •    我们可以不考虑输出结果的顺序。

跟进:

  • 如果给定的数组已经排好序呢?你将如何优化你的算法?
  • 如果 nums1 的大小比 nums2 小很多,哪种方法更优?
  • 如果nums2的元素存储在磁盘上,内存是有限的,你不能一次加载所有的元素到内存中,你该怎么办?

解题思路:

    第一反应是在349那道题的基础上,把del那行删了就行,还是太年轻了。。。如果输入是[1]. [1,1]就不行了,还是得先将两个数组排序然后,同时遍历两个数组,并设置i,j两个变量。


解题代码:

        ans=[]
        nums1.sort()
        nums2.sort()
        i=j=0
        while i<len(nums1) and j<len(nums2):
            if nums1[i]<nums2[j]:
                i+=1
            elif nums2[j]<nums1[i]:
                j+=1
            else:
                ans.append(nums2[j])
                i+=1
                j+=1
        return ans

### Wise Inner Shape IoU Implementation and Usage In the context of computer vision or 3D shape analysis, Wise Inner Shape Intersection over Union (IoU) serves as an evaluation metric that measures how well two shapes align with each other internally. This method focuses on comparing inner structures rather than outer boundaries to assess similarity more accurately. The formula for calculating this specific type of IoU can be defined as follows: \[ \text{WiseInnerShapeIoU} = \frac{\left| A_{inner} \cap B_{inner}\right|}{\left|A_{inner} \cup B_{inner}\right|} \] where \(A_{inner}\) represents the set of points inside object A but not part of its boundary, while \(B_{inner}\) denotes those within object B under similar conditions[^1]. For practical implementation purposes, one approach involves converting both objects into voxel grids where occupied cells represent solid parts whereas empty ones signify void spaces between surfaces. Afterward, operations such as intersection (\(\cap\)) and union (\(\cup\)) become straightforward bitwise logic applied across corresponding voxels from either grid. Below is a Python code snippet demonstrating how to compute Wise Inner Shape IoU using NumPy arrays representing binary volumes: ```python import numpy as np def calculate_wise_inner_shape_iou(volume_a: np.ndarray, volume_b: np.ndarray) -> float: """ Calculate the wise inner shape IoU given two binary volumetric representations. Args: volume_a (np.ndarray): Binary array indicating presence/absence of material in Volume A. volume_b (np.ndarray): Similar representation for Volume B. Returns: float: Computed value of Wise Inner Shape IoU ranging [0., 1.]. """ # Ensure inputs are boolean-valued ndarrays mask_a = volume_a.astype(bool) mask_b = volume_b.astype(bool) # Compute intersections & unions considering only internal regions intersec = ((mask_a ^ (~volume_a)) * (mask_b ^ (~volume_b))).sum() union = (((mask_a | ~volume_a) | (mask_b | ~volume_b)) != False).sum() return max(0., min(intersec / union, 1.)) ``` This function takes two three-dimensional Numpy arrays (`volume_a`, `volume_b`) containing zeros and ones which indicate whether certain positions belong to respective solids' interiors. It returns a floating-point number signifying their degree of overlap according to the described criterion. --related questions-- 1. How does changing resolution affect accuracy when computing Wise Inner Shape IoU? 2. What preprocessing steps should precede applying this technique effectively? 3. Can you provide examples illustrating scenarios wherein traditional metrics fail compared against Wise Inner Shape IoU? 4. Are there alternative methods offering comparable performance yet simpler computation requirements?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值