Conversation
|
レビュー依頼のリンク、張り間違えているようです。修正いただけますか? |
|
大変失礼しました。。 |
| if len(num_counter) == k: | ||
| return [num for num, _ in num_counter.items()] | ||
|
|
||
| pairs_of_count_num = [] |
There was a problem hiding this comment.
上でpairs_of_count_and_numとしていたのですが、抜け落ちたようです。。
|
|
||
| class Solution: | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| num_counter = defaultdict(int) |
There was a problem hiding this comment.
num_counter ですと、カウンターの数を表現しているように感じました。 num_to_counter あたりがよいと思います。
There was a problem hiding this comment.
盲点でした。
ここら辺の感覚がまるで無く、、
意識して使い分けてみようと思います。
There was a problem hiding this comment.
Counterというクラスもありますね。https://docs.python.org/3/library/collections.html#collections.Counter
There was a problem hiding this comment.
counterという命名だと、紛らわしいでしょうか。
There was a problem hiding this comment.
スコープが短い場合は、前後をざっと読んで、数字をカウントしているということが分かりますので、問題ないと思います。スコープがある程度長い場合は、何をカウントしているかを変数名で表したほうが良いと思います。
| num_counter[num] += 1 | ||
|
|
||
| if len(num_counter) == k: | ||
| return [num for num, _ in num_counter.items()] |
There was a problem hiding this comment.
ありがとうございます。
ご指摘のとおりですね。
|
nodchipさん、liquo-riceさん、Mike0121さん、レビューを有難うございました! |
https://leetcode.com/problems/top-k-frequent-elements/description/