-
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
Conversation
| def intersection(self, nums1, nums2): | ||
|
|
||
| # Initialize seen dictionary and res array | ||
| seen = {} |
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.
1, 0 いれているのが不自然ですね。せめて、True False ですが、そうすると、True が seen に見えませんか。
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.removeを使うのがいい気がします。
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.
自分もそう思います
コードとそれに対する考えを書くべきだと思っていたので、leetcodeにあったコードをそのまま持ってきてしまいました
自分が書いたコード以外を載せるとややこしいので、次から載せないようにしますね
| for x in nums1: | ||
| seen[x] = 1 |
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.
内包表記で書く方法がありますね。
| long_nums = nums1 | ||
|
|
||
| unique_short_nums = set(short_nums) | ||
| seen = 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.
seen を使わずに unique_short_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.
removeする方法もありますね、ありがとうございます
| if nums1[i1] < nums2[i2]: | ||
| i1 += 1 | ||
| continue | ||
| if nums2[i2] < nums1[i1]: |
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.
趣味の範囲ですが、個人的には if nums1[i1] > nums2[i2]: と書きます。
パターンとして nums1[i1] が nums2[i2] と比較して、小さい、大きい、(等しい)、と読めた方がスムーズな気がします。
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.
数直線上に一直線に並ぶように < を優先的に使う流派と、比較される数を左側に持ってくる流派があるように思います。自分は前者に近いです。
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.
好みかなとは思うのですが、自分も数直線をイメージして < を使うことが多いです!
| i2 += 1 | ||
| continue | ||
| result.append(nums1[i1]) | ||
| val = nums1[i1] |
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.append(val) と書くとより明確になると思いました。
| else: | ||
| short_nums = nums2 | ||
| long_nums = nums1 | ||
| short_nums_set = set(short_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.
個人的な好みですが、前処理と実際に答えを求めるループの間に空行がある方が処理がまとまって読みやすいと感じます。
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.
以前PEP8を読んだときに関数内で使わない方が良いと書いてあった気がしたんですが勘違いでした
「関数内では、論理的なセクションを示すために、空白行を(控えめに)使用します」
という記述でした
このケースだと使った方が分かりやすいですね
| 2. 片方がとても大きくてかつソート済み、もう片方がとても小さい場合 | ||
|
|
||
| この場合は小さい方をloopし、二分探索で大きい方に存在するかどうか確認すればいい。 | ||
| 時間計算量はO(min(m, n) * log(max(m, n)))になり、空間計算量はO(1)(計算のために確保する変数はleft, rightくらいのため) |
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.
nit: resultを保持していないといけないので、最悪空間計算量は O(min(m, n))であるとした方が一応、正確かもしれません。
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に加えてlong_numsも保持していますし、空間計算量はO(max(m, n))になりそうですね、、
ありがとうございます
| if long_nums[mid] == num: | ||
| result.add(num) | ||
| break | ||
| elif long_nums[mid] < num: |
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.
breakしてるのでelifじゃなくてifでいい気がします。
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| if len(nums1) < len(nums2): |
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.
len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1みたいな感じの文だけを入れてshort_numsをnums1, long_numsをnums2に置き換えて書く選択肢もありそうです。この方が簡潔にはなりますかね。
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が長いという意味が追加されてしまうのが気になったので冗長ですがこのような書き方を選択しました
問題:349. Intersection of Two Arrays
次の問題:929. Unique Email Addresses