Skip to content

349. Intersection Of Two Arrays#18

Open
Yuto729 wants to merge 1 commit intomainfrom
intersection-of-two-arrays
Open

349. Intersection Of Two Arrays#18
Yuto729 wants to merge 1 commit intomainfrom
intersection-of-two-arrays

Conversation

@Yuto729
Copy link
Copy Markdown
Owner

@Yuto729 Yuto729 commented Dec 10, 2025

解く問題

Intersection Of Two Arrays

次に解く問題

Unique Email Addresses

@Yuto729 Yuto729 changed the title Intersection Of Two Arrays 349. Intersection Of Two Arrays Dec 10, 2025
Repository owner deleted a comment from github-actions bot Dec 10, 2025
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
set1 = set(nums1)
set2= set(nums2)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

= の両側にスペースを空けることをおすすめします。

if nums2[j] < nums1[i]:
j += 1
continue
common = nums1[i]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

132 行目に空行を入れているのであれば、ここにも空行を入れるのが自然だと思いました。

Comment on lines +151 to +154
nums1_set = set(nums1)
nums2_set = set(nums2)
# nums1_set.intersection(nums2_set)と同じ
return list(nums1_set & nums2_set)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nums*_set 変数に代入する理由があまりなさそうなので、そのままsetにしても良いのかなと思いました。& を利用するその場でsetにしていることが分かるというメリットも得られそうです。

Suggested change
nums1_set = set(nums1)
nums2_set = set(nums2)
# nums1_set.intersection(nums2_set)と同じ
return list(nums1_set & nums2_set)
# return list(set(nums1).intersection(set(nums2)))
return list(set(nums1) & (set(nums2)))

seen.add(num)
intersection = set()
for num in nums2:
if num in seen and num not in intersection:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num not in intersectionの部分は不要かと思います。

nums2.sort()
i, j = 0, 0
intersection = set()
last = None
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この変数は未使用でしょうか?

j += 1
continue

common = nums1[i]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

共通部分を先に処理してから2つのポインターを動かす方法もございます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants