349. Intersection of Two Arrays#13
Open
tarinaihitori wants to merge 1 commit into
Open
Conversation
liquo-rice
reviewed
Nov 2, 2024
| 1st | ||
| それぞれの配列の要素を set に格納し、両方の set に含まれているものを結果用の set に格納すればいけそう。 | ||
| 時間計算量:O(n + m) num1 の長さを n、nums2 の長さを m とする | ||
| 空間計算量:O(n + m) |
Owner
Author
There was a problem hiding this comment.
nums1とnums2の長さを比較して、短い方だけをsetに格納すれば
O(min(n, m))の空間計算量で書くことができると思います。以下のような感じでしょうか。
class Solution:
def intersection(nums1, nums2):
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
set_nums = set(nums1) # 小さい方の配列をセットに変換
result = set()
for num in nums2:
if num in set_nums:
result.add(num)
return list(result)
Owner
Author
There was a problem hiding this comment.
結果を返すresultをsetからリストに変えて、重複を見つけたら、set_numsからremoveするようにしました。
これ以外だと、時間計算量はO(n * m)になりますが、二重ループでそれぞれ値を取り出して値が同じならsetに格納する方法が思いつきました。
class Solution:
def intersection(self, nums1, nums2):
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
set_nums = set(nums1)
result = []
for num in nums2:
if num in set_nums:
result.append(num)
set_nums.remove(num)
return resultThere was a problem hiding this comment.
Pythonは引数が値渡しなので気にしなくてもいいと思いますが、参照渡しの言語なら下のようにすると参照透過性が失われないと思いました
if len(nums1) > len(nums2):
return self.intersection(nums2, nums1)| 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.
好みですが、それぞれの set を変数化する必要もないのかなと思いました。そのぐらい読みやすいです。
oda
reviewed
Nov 3, 2024
| ``` | ||
|
|
||
| 3rd | ||
| &演算子が一番完結、かつ、bit 演算の&と同じで積を取っていることがわかりやすいので、こちらを採用 |
There was a problem hiding this comment.
他、両方を sort するという手もあるでしょう。
そこから、merge sort ようなことをする作戦や、片方の値でバイナリサーチをする作戦などがあるでしょう。
あと、他の人のコードを読むほうが練習として大事なので、Discord サーバー内を漁るといいと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/intersection-of-two-arrays/description/