Skip to content

349. Intersection Of Two Arrays#15

Open
xbam326 wants to merge 3 commits into
mainfrom
349
Open

349. Intersection Of Two Arrays#15
xbam326 wants to merge 3 commits into
mainfrom
349

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Jan 13, 2026

解く問題
Intersection Of Two Arrays

次に解く問題
Unique Email Addresses

- 追加質問の例なども勉強になった。


- suggestされて気になって調べたが、`intersection_update`という呼び出しもとの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.

https://docs.python.org/3/library/stdtypes.html#set.intersection_update
こういうリンクを残しておいてもいいと思います。

Comment on lines +10 to +11
if len(nums1) > len(nums2):
return self.intersection(nums2, nums1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

小さい方をセットにしたいんだよ、という気持ちは以下のようにも書けそうです。

Suggested change
if len(nums1) > len(nums2):
return self.intersection(nums2, nums1)
smaller = nums1
larger = nums2
if len(smaller) > len(larger):
smaller, larger = larger, smaller

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

再帰を使わないのであれば、個人的には以下のように書きたいです。

Suggested change
if len(nums1) > len(nums2):
return self.intersection(nums2, nums1)
smaller, larger = sorted([nums1, nums2], key=len)

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