-
Notifications
You must be signed in to change notification settings - Fork 0
Intersection of the two arrays #33
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: arai60
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,5 @@ | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| nums1 = set(nums1) | ||
| nums2 = set(nums2) | ||
| return list(nums1 & nums2) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| """ | ||
| Reference | ||
| ryotaro: https://github.com/Ryotaro25/leetcode_first60/pull/14/files | ||
| mike: https://github.com/Mike0121/LeetCode/pull/30/files | ||
| kazukiii: https://github.com/kazukiii/leetcode/pull/14/files | ||
| 本題ではないが, pythonのsortedとlist.sort()の計算中に空間計算量O(n)を消費する情報を得られてラッキー | ||
|
|
||
| numpy実装も確認したが,,,python側から見えるのはnp.arrayが実装されている前提のものだった | ||
| https://github.com/numpy/numpy/blob/v2.0.0/numpy/lib/_arraysetops_impl.py#L578-L668 | ||
| """ | ||
|
|
||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| return list(set(nums1) & set(nums2)) | ||
|
|
||
| # sortを用いて | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| nums1 = sorted(nums1) | ||
| nums2 = sorted(nums2) | ||
| intersection_of_the_two = [] | ||
| seen = set() | ||
| i1 = 0 | ||
| i2 = 0 | ||
| while i1 < len(nums1) and i2 < len(nums2): | ||
| if nums1[i1] == nums2[i2]: | ||
| if nums1[i1] not in seen: | ||
| intersection_of_the_two.append(nums1[i1]) | ||
| seen.add(nums1[i1]) | ||
|
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. 最初にソートしていて同じ値のものは隣合っていることが保証されているので、ポインタをうまく移動させることで重複排除にseenを使わなくても済み、ちょっとメモリを節約できます。 具体的には以下のような感じです。 while i1 < len(nums1) and i2 < len(nums2):
if nums1[i1] == nums2[i2]:
common = nums1[i1]
intersection_of_the_two.append(common)
while i1 < len(nums1) and nums1[i1] == common:
i1 += 1
while i2 < len(nums2) and nums2[i2] == common:
i2 += 1
elif nums1[i1] < nums2[i2]:
i1 += 1
else:
i2 += 1
Owner
Author
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. ソートしているのでseenがなくても重複している値はわかるというのは盲点でした。とてもスッキリした考察ありがとうございます。seen書くのは空間計算量が増えるので結構ためらって書いたので助かります。 |
||
| i1 += 1 | ||
| i2 += 1 | ||
| elif nums1[i1] < nums2[i2]: | ||
| i1 += 1 | ||
| else: | ||
| i2 += 1 | ||
| return intersection_of_the_two | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| return list(set(nums1) & set(nums2)) | ||
|
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.
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.
Owner
Author
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. operator & を set classがメソッドとして持っていると推測してるのですが<-これであっていると思います。コードは上のCPythonにあります 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. なるほど ありがとうございます 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. 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. C++ は operator ですね。 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. C++のような文法を予想して探してたのですぐに見つかりませんでした 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. Rust は、これですかね? 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. ですです
これは知らなかった。なんでなのか調べるの面白そうですね |
||
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の場合正確には「参照の値渡し」だったので、この書き方で問題なかったです。
このコメントは無視してください。