Intersection of Two Arrays——Array
程序员文章站
2024-02-29 17:43:34
...
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
re = []
n = 0
for i in range(len(nums1)):
n |= 1<<nums1[i]
for i in range(len(nums2)):
if n&(1<<nums2[i]):
n &= (~(1<<nums2[i]))
re.append(nums2[i])
return re
上一篇: 字符串---删除子串(1)
推荐阅读
-
Intersection of Two Arrays——Array
-
Leetcode题解 4. Median of Two Sorted Arrays 【Array】
-
array- Median of Two Sorted Arrays
-
leetcode.array--4. Median of Two Sorted Arrays
-
【leetcode】#4 Median of Two Sorted Arrays【Array】【Hard】
-
java基础之Collection与Collections和Array与Arrays的区别
-
java基础之Collection与Collections和Array与Arrays的区别
-
php-Arrays函数-array_intersect_ukey-用回调函数比较键名来计算数组的交集_PHP教程
-
php-Arrays函数-array_rand-从数组中随机取出一个或多个单元_PHP教程
-
LeetCode 160 - Intersection of Two Linked Lists