From a93ca47fbdd40acd608c56cc8cc3bf896ce9b07a Mon Sep 17 00:00:00 2001 From: mt2324 <63892273+mt2324@users.noreply.github.com> Date: Mon, 29 Jun 2026 19:01:40 +0900 Subject: [PATCH] Create 349. Intersection of Two Arrays.md https://leetcode.com/problems/intersection-of-two-arrays/ --- .../349. Intersection of Two Arrays.md | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 349. Intersection of Two Arrays/349. Intersection of Two Arrays.md diff --git a/349. Intersection of Two Arrays/349. Intersection of Two Arrays.md b/349. Intersection of Two Arrays/349. Intersection of Two Arrays.md new file mode 100644 index 0000000..15b8715 --- /dev/null +++ b/349. Intersection of Two Arrays/349. Intersection of Two Arrays.md @@ -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を取れるらしい。 +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)) +```