avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

350.The intersection of two array

  2024-01-01        
字数统计: 362字   |   阅读时长: 2min
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
350. The intersection of two array II
Simple
883
company
Amazon
company
Byte beating
company
Facebook
Give you two integer arrays nums1 and nums2 ,Please return the intersection of two arrays in the form of an array。The number of times each element in the return result,It should be consistent with the number of times the elements appear in both arrays(If the number of times is inconsistent,Then consider taking the smaller value)。You can consider the order of the output result。



Exemplary example 1:

enter:nums1 = [1,2,2,1], nums2 = [2,2]
Output:[2,2]
Exemplary example 2:

enter:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output:[4,9]


hint:

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


Advance:

If the given array has been arranged, the order is good?How will you optimize your algorithm?
if nums1 Size ratio nums2 Small,Which method is better?
if nums2 The elements are stored on the disk,Memory is limited,And you can't load all elements at one time to memory,what should you do?

350_fig1.gif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
hash_1 = {}
hash_2 = {}
for index, i in enumerate(nums):
if i not in hash_1:
hash_1[i] = 1
hash_2[i] = [index]
else:
hash_1[i] += 1
hash_2[i].append(index)
for i in hash_1:
if hash_1[i] >= 2:
for j in range(len(hash_2[i])):
for m in range(j + 1, len(hash_2[i])):
if abs(hash_2[i][j] - hash_2[i][m]) <= k:
return True
return False
1
2
3
4
5
6
7
8
9
10
11
12
# Sort out the meaning:Whether the existence does not exceed k+1k + 1k+1 window,window内有相同元素。
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
n = len(nums)
s = set()
for i in range(n):
if i > k:
s.remove(nums[i - k - 1])
if nums[i] in s:
return True
s.add(nums[i])
return False

&&Operate

1
2
3
4
5
6
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
num1 = collections.Counter(nums1)
num2 = collections.Counter(nums2)
num = num1 & num2
return num.elements()
  • Python
  • Hash table
  • solved
  • Double pointer

扫一扫,分享到微信

微信分享二维码
2341. How much can the array be formed One question daily
345. Voice letter in the reverse string
目录
ahoh, this article has no catalog.

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号