Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions 349. Intersection of Two Arrays/349. Intersection of Two Arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
https://leetcode.com/problems/intersection-of-two-arrays/
## STEP 1
素直に両方setに変換してintersectionを取る。メモリはO(n)追加でいるけど速い。
```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
num1_set = set(nums1)
num2_set = set(nums2)
ans = []
for num in num1_set.intersection(num2_set):
ans.append(num)
return ans
```

ソートしてtwo pointersだけど複数回同じ数字が被った時のために結局setを作んないといけない。あと地味にソートも重い。intersectionが少なくてメモリをできるだけ節約したい時にはいいのか?

```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums1.sort()
nums2.sort()
i = 0
j = 0
intersection = set()
while i < len(nums1) and j < len(nums2):
if nums1[i] < nums2[j]:
i += 1
elif nums2[j] < nums1[i]:
j += 1
else:
intersection.add(nums1[i])
i += 1
j += 1
return list(intersection)
```

Counter で各要素を数えることもできるけどこれは本質的にsetに変換してるのと代わりない。
```python
from collections import Counter
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
counter1 = Counter(nums1)
counter2 = Counter(nums2)
intersection = []
for num1 in counter1.keys():
if num1 in counter2:
intersection.append(num1)
return intersection
```

## STEP2
intersectionは`&`でも`.intersection()`でも取れるが`&`だとset同士でしか取れないが、`.intersection()` だと引数にset以外のiterable objectを取れるらしい。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

まだでしたら CPython の該当箇所を読んでみると面白いと思います。

https://docs.python.org/3.14/library/stdtypes.html#set-types-set-frozenset

あとsetを生成するはhashを計算する必要があって時間がかかるので小さい方をsetにした方がいいのはたしかにと思った。

```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
if len(nums1) < len(nums2):
return list(set(nums1).intersection(nums2))
else:
return list(set(nums2).intersection(nums1))
```
ついでにCounterを使った方も書きなおしたり、
```python
from collections import Counter
class Solution:

def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
intersection = ()
if len(nums1) < len(nums2):
counter = Counter(nums1)
nums_to_iterate = nums2
else:
counter = Counter(nums2)
nums_to_iterate = nums1

intersection = []
for num in nums_to_iterate:
if num in counter and counter[num] > 0:
intersection.append(num)
counter[num] = 0
return intersection
```
分岐なしで書くならこうかな。
```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list(set(nums1) & set(nums2))
```
## STEP3
練習するだけ
```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
if len(nums1) < len(nums2):
return list(set(nums1).intersection(nums2))
else:
return list(set(nums2).intersection(nums1))
```