349. Intersection of Two Arrays - #7
Conversation
|
|
||
| return res | ||
| ``` | ||
| - Leetcodeのruntimeは0msになっていて、ほんとうにこれ信用できない代物だなあという感を新たにする。 |
There was a problem hiding this comment.
LeetCode の実行時間、確かにブレが激しいですよね。
試しに手で計算してみるといいかもしれません。
Yuto729/leetcode#16 (comment)
kazizi55/coding-challenges#10 (comment)
| res = [] | ||
|
|
||
| for num in nums1: | ||
| num_to_seen[num] = 1 |
There was a problem hiding this comment.
num_to_seen と聞くと、すでにみた数字かどうかという bool が value に入るように感じるので、個人的にはnum_to_seen[num] = Trueにするのが自然に思えました。
また、実際は nums1 の頻度を格納しているので、num1_to_seenとする方が親切かもしれません。
今のまま頻度を格納したいなら、num_to_frequency とする方が実態に即しているかもです。
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| num_to_seen = {} | ||
| res = [] |
There was a problem hiding this comment.
num_to_frequency に変えて frequency が2以上の num の配列を返すようにすれば res なしでも解けると思います。
|
|
||
| ### コメント集 | ||
| - https://github.com/quinn-sasha/leetcode/pull/13#discussion_r1960884543 | ||
| > この問題は問題文自体では終わっていなくて、解けた後に、いくつか追加の条件が出てきて、その下でのアルゴリズムとそれらの pros and cons が要求されると思います。 |
There was a problem hiding this comment.
もうご覧になっているかもしれませんが、binary search を使う解法もあります。
katataku/leetcode#12 (comment)
There was a problem hiding this comment.
*.py の内容が memo.md と重複しているので手元で実行したログを残しておきたいなどの理由がない限りは memo.md に統一しても良いのではと思いました。
https://leetcode.com/problems/intersection-of-two-arrays/description/