Skip to content

349. Intersection of two arrays#15

Open
nittoco wants to merge 1 commit intomainfrom
nittoco-patch-15
Open

349. Intersection of two arrays#15
nittoco wants to merge 1 commit intomainfrom
nittoco-patch-15

Conversation

@nittoco
Copy link
Copy Markdown
Owner

@nittoco nittoco commented Jun 1, 2024

問題文URL: https://leetcode.com/problems/intersection-of-two-arrays/description/

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

問題文URL: https://leetcode.com/problems/intersection-of-two-arrays/description/

Given two integer arrays nums1 and nums2, return an array of their  intersection. Each element in the result must be unique and you may return the result in any order.
if nums1[index_1] > nums2[index_2]:
index_2 += 1
continue
common.add(nums1[index_1])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

両方に、例えば5が2つずつ入っていたら、2回 add されるの一応気になります。(set にいれているので、結果的には問題ないわけですが、本当は list への追記にすることもできて、そうすると最後の list へのキャストが省略できますよね。)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

これ、反応として気になるなという感じなんですね。ありがとうございます。
後でlistのバージョンで実装してみます

Copy link
Copy Markdown

@liquo-rice liquo-rice left a comment

Choose a reason for hiding this comment

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

良さそうです。

index_2 = 0
nums1 = sorted(nums1)
nums2 = sorted(nums2)
common = 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.

setを使わないで書けますか?

```

- 片方を、dict か set にする
- 変数名、elementとか回りくどく説明しなくても、これくらいなら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.

むしろelementよりnumの方が情報が多い気がします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

確かにそうですね

def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums2 = set(nums2)
common = set()
for element_1 in 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.

数字の前にunderscoreを入れる方が一般的ですか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

grep.app でPythonに絞って検索したら、num_1が734件、num1が3180件なので、underscoreつけないのがむしろ一般的なのかもしれないですね。
liquo_riceさんなら、こういうことを調べる時どこで調べますか?

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://source.chromium.org/https://cs.opensource.google/ ですかね。underscoreは単語の区切りを明確にするためのものだとみなすと、数字の前後には不要な気がします。

Copy link
Copy Markdown
Owner Author

@nittoco nittoco Jun 2, 2024

Choose a reason for hiding this comment

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

おお、このようなページがあるんですね、ありがとうございます。

数字の前後には不要な気がします。

確かにそうですね。num2ではなく、num_twoのような表記だったらあっても良さそうです

Comment on lines +12 to +14
unique_nums1 = set(nums1)
unique_nums2 = set(nums2)
return list(unique_nums1 & unique_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.

無理に変数に入れずに

return list(set(nums1) & set(nums2))

みたいにしてしまってもいいかもですね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

その選択肢は思いついてなかったです。ありがとうございます。

Comment on lines +85 to +93
```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums2 = set(nums2)
common = set()
for num in nums1:
if num in nums2:
common.add(num)
return list(common)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        nums2 = set(nums2)
        common = []
        for num in nums1:
            if num in nums2 and num not in common:
                common.append(num)
        return common

(他の選択肢として)このようにすればcommonを最初からlistでいけますね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほどそうですね、ありがとうございます
みなさんのコメントを見てて思うんですが、setに変換したりlistに変換したりするのって意外と抵抗があるものなんですね

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