-
Notifications
You must be signed in to change notification settings - Fork 0
349. Intersection of Two Arrays #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| 1st | ||
| それぞれの配列の要素を set に格納し、両方の set に含まれているものを結果用の set に格納すればいけそう。 | ||
| 時間計算量:O(n + m) num1 の長さを n、nums2 の長さを m とする | ||
| 空間計算量:O(n + m) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| unique_nums1 = set() | ||
| unique_nums2 = set() | ||
| intersection_result = set() | ||
|
|
||
| for num in nums1: | ||
| unique_nums1.add(num) | ||
|
|
||
| for num in nums2: | ||
| unique_nums2.add(num) | ||
|
|
||
| for num in unique_nums1: | ||
| if num in unique_nums2: | ||
| intersection_result.add(num) | ||
|
|
||
| return list(intersection_result) | ||
|
|
||
| ``` | ||
|
|
||
| 2nd | ||
| set について調べていると、 | ||
| `intersection()`というメソッドがあったのでこれも使える。 | ||
|
|
||
| https://docs.python.org/ja/3/library/stdtypes.html#frozenset.intersection | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| num1_set = set(nums1) | ||
| num2_set = set(nums2) | ||
| return list(num1_set.intersection(num2_set)) | ||
| ``` | ||
|
|
||
| &でも積集合を求めることができるらしい。 | ||
| 気になったので、ここの実装を調べてみた。 | ||
| & 演算子は、Python の set クラスでオーバーロードされており、**and** メソッドを呼び出していた。 | ||
|
|
||
| https://github.com/python/cpython/blob/bd4be5e67de5f31e9336ba0fdcd545e88d70b954/Lib/_collections_abc.py#L60 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| return list(set(nums1) & set(nums2)) | ||
| ``` | ||
|
|
||
| 3rd | ||
| &演算子が一番完結、かつ、bit 演算の&と同じで積を取っていることがわかりやすいので、こちらを採用 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 他、両方を sort するという手もあるでしょう。 あと、他の人のコードを読むほうが練習として大事なので、Discord サーバー内を漁るといいと思います。 |
||
|
|
||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| nums1_set = set(nums1) | ||
| nums2_set = set(nums2) | ||
| return list(nums1_set & nums2_set) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好みですが、それぞれの set を変数化する必要もないのかなと思いました。そのぐらい読みやすいです。 |
||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
空間計算量がO(min(n, m))の方法でできますか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nums1とnums2の長さを比較して、短い方だけをsetに格納すれば
O(min(n, m))の空間計算量で書くことができると思います。以下のような感じでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setを一つだけ(set_numsのみ)使ってできますか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
結果を返すresultをsetからリストに変えて、重複を見つけたら、set_numsからremoveするようにしました。
これ以外だと、時間計算量はO(n * m)になりますが、二重ループでそれぞれ値を取り出して値が同じならsetに格納する方法が思いつきました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良さそうです!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pythonは引数が値渡しなので気にしなくてもいいと思いますが、参照渡しの言語なら下のようにすると参照透過性が失われないと思いました