Skip to content

Conversation

@garunitule
Copy link
Owner

def intersection(self, nums1, nums2):

# Initialize seen dictionary and res array
seen = {}
Copy link

Choose a reason for hiding this comment

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

1, 0 いれているのが不自然ですね。せめて、True False ですが、そうすると、True が seen に見えませんか。

Copy link

Choose a reason for hiding this comment

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

普通にset()とset.removeを使うのがいい気がします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

自分もそう思います
コードとそれに対する考えを書くべきだと思っていたので、leetcodeにあったコードをそのまま持ってきてしまいました
自分が書いたコード以外を載せるとややこしいので、次から載せないようにしますね

Comment on lines +76 to +77
for x in nums1:
seen[x] = 1
Copy link

Choose a reason for hiding this comment

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

内包表記で書く方法がありますね。

long_nums = nums1

unique_short_nums = set(short_nums)
seen = set()
Copy link

Choose a reason for hiding this comment

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

seen を使わずに unique_short_nums から削除する手もありますね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

removeする方法もありますね、ありがとうございます

if nums1[i1] < nums2[i2]:
i1 += 1
continue
if nums2[i2] < nums1[i1]:

Choose a reason for hiding this comment

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

趣味の範囲ですが、個人的には if nums1[i1] > nums2[i2]: と書きます。
パターンとして nums1[i1] が nums2[i2] と比較して、小さい、大きい、(等しい)、と読めた方がスムーズな気がします。

Copy link

@huyfififi huyfififi May 26, 2025

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.

数直線上に一直線に並ぶように < を優先的に使う流派と、比較される数を左側に持ってくる流派があるように思います。自分は前者に近いです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

好みかなとは思うのですが、自分も数直線をイメージして < を使うことが多いです!

i2 += 1
continue
result.append(nums1[i1])
val = nums1[i1]

Choose a reason for hiding this comment

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

上の行と入れ替えて、result.append(val) と書くとより明確になると思いました。

else:
short_nums = nums2
long_nums = nums1
short_nums_set = set(short_nums)

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.

以前PEP8を読んだときに関数内で使わない方が良いと書いてあった気がしたんですが勘違いでした

「関数内では、論理的なセクションを示すために、空白行を(控えめに)使用します」
という記述でした
このケースだと使った方が分かりやすいですね

https://peps.python.org/pep-0008/#blank-lines:~:text=Use%20blank%20lines%20in%20functions%2C%20sparingly%2C%20to%20indicate%20logical%20sections.

2. 片方がとても大きくてかつソート済み、もう片方がとても小さい場合

この場合は小さい方をloopし、二分探索で大きい方に存在するかどうか確認すればいい。
時間計算量はO(min(m, n) * log(max(m, n)))になり、空間計算量はO(1)(計算のために確保する変数はleft, rightくらいのため)

Choose a reason for hiding this comment

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

nit: resultを保持していないといけないので、最悪空間計算量は O(min(m, n))であるとした方が一応、正確かもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

resultに加えてlong_numsも保持していますし、空間計算量はO(max(m, n))になりそうですね、、
ありがとうございます

if long_nums[mid] == num:
result.add(num)
break
elif long_nums[mid] < num:
Copy link

Choose a reason for hiding this comment

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

breakしてるのでelifじゃなくてifでいい気がします。

```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
if len(nums1) < len(nums2):
Copy link

@potrue potrue May 26, 2025

Choose a reason for hiding this comment

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

len(nums1) > len(nums2):
  nums1, nums2 = nums2, nums1

みたいな感じの文だけを入れてshort_numsをnums1, long_numsをnums2に置き換えて書く選択肢もありそうです。この方が簡潔にはなりますかね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかに簡潔ですね
nums1が短くnums2が長いという意味が追加されてしまうのが気になったので冗長ですがこのような書き方を選択しました

@garunitule garunitule merged commit 0c8b1b2 into main Jun 30, 2025
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.

7 participants