Skip to content

Conversation

@SuperHotDogCat
Copy link
Owner

Comment on lines +3 to +4
nums1 = set(nums1)
nums2 = set(nums2)

Choose a reason for hiding this comment

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

入力を破壊しても良いかどうかは確認した方がいいかもしれません。

Choose a reason for hiding this comment

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

すみません、Pythonの場合正確には「参照の値渡し」だったので、この書き方で問題なかったです。
このコメントは無視してください。

if nums1[i1] == nums2[i2]:
if nums1[i1] not in seen:
intersection_of_the_two.append(nums1[i1])
seen.add(nums1[i1])

Choose a reason for hiding this comment

The 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

Copy link
Owner Author

Choose a reason for hiding this comment

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

ソートしているのでseenがなくても重複している値はわかるというのは盲点でした。とてもスッキリした考察ありがとうございます。seen書くのは空間計算量が増えるので結構ためらって書いたので助かります。

@@ -0,0 +1,3 @@
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list(set(nums1) & set(nums2))

Choose a reason for hiding this comment

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

&intersection()と同値なことができるのってどうしてなんですか??
operator & を set classがメソッドとして持っていると推測してるのですが、ソースコード見つけれられず

Choose a reason for hiding this comment

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

operator & を set classがメソッドとして持っていると推測してるのですが<-これであっていると思います。コードは上のCPythonにあります

Choose a reason for hiding this comment

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

なるほど ありがとうございます
__and__って書くんですね(^^ ;) grepしても見つからないわけだ

Copy link

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

C++のような文法を予想して探してたのですぐに見つかりませんでした

Copy link

Choose a reason for hiding this comment

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

Rust は、これですかね?
https://doc.rust-lang.org/std/ops/index.html

Choose a reason for hiding this comment

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

ですです

Note that the && and || operators are currently not supported for overloading. Due to their short circuiting nature, they require a different design from traits for other operators like BitAnd. Designs for them are under discussion.

これは知らなかった。なんでなのか調べるの面白そうですね

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.

6 participants