Skip to content

349 intersection of two arrays#13

Open
kitano-kazuki wants to merge 2 commits intomainfrom
349-intersection-of-two-arrays
Open

349 intersection of two arrays#13
kitano-kazuki wants to merge 2 commits intomainfrom
349-intersection-of-two-arrays

Conversation

@kitano-kazuki
Copy link
Copy Markdown
Owner

Comment on lines +3 to +8
if len(nums1) < len(nums2):
smaller_nums = nums1
larger_nums = nums2
else:
smaller_nums = nums2
larger_nums = 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.

配列の大きさを命名で明示的にしないでよいならば、

if len(nums1) > len(nums2):
        nums1, nums2 = nums2, nums1

で済みますね

Comment on lines +15 to +19
result_set = set()
for num in larger_nums:
if num not in exist_in_smaller:
continue
result_set.add(num)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

result は hash set を使わずにも書けます。
hash set の remove, discard が使えます。
https://docs.python.org/3.14/library/stdtypes.html#set.remove

Comment on lines +114 to +116
exist_in_smaller = set()
for num in smaller_nums:
exist_in_smaller.add(num)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

exist_in_smaller = set(smaller_nums) と書けますね。

exist_in_smaller = set(smaller_nums)
exist_in_larger = set(larger_nums)

result = exist_in_smaller.intersection(exist_in_larger)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

exist_in_smaller & exist_in_larger と書いたほうがシンプルだと思います。

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.

3 participants