Skip to content

347. Top K Frequent Elements#11

Open
TORUS0818 wants to merge 3 commits intomainfrom
347
Open

347. Top K Frequent Elements#11
TORUS0818 wants to merge 3 commits intomainfrom
347

Conversation

@TORUS0818
Copy link
Copy Markdown
Owner

@nodchip
Copy link
Copy Markdown

nodchip commented May 24, 2024

レビュー依頼のリンク、張り間違えているようです。修正いただけますか?

@TORUS0818
Copy link
Copy Markdown
Owner Author

大変失礼しました。。
修正いたしました。

if len(num_counter) == k:
return [num for num, _ in num_counter.items()]

pairs_of_count_num = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら count_and_nums と付けると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

上でpairs_of_count_and_numとしていたのですが、抜け落ちたようです。。


class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
num_counter = defaultdict(int)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num_counter ですと、カウンターの数を表現しているように感じました。 num_to_counter あたりがよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

盲点でした。
ここら辺の感覚がまるで無く、、

意識して使い分けてみようと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Counterというクラスもありますね。https://docs.python.org/3/library/collections.html#collections.Counter

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

counterという命名だと、紛らわしいでしょうか。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

スコープが短い場合は、前後をざっと読んで、数字をカウントしているということが分かりますので、問題ないと思います。スコープがある程度長い場合は、何をカウントしているかを変数名で表したほうが良いと思います。

num_counter[num] += 1

if len(num_counter) == k:
return [num for num, _ in num_counter.items()]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num_counter.values() でよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
ご指摘のとおりですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

確認しましたが、全体的に良いと思いました。

@TORUS0818
Copy link
Copy Markdown
Owner Author

nodchipさん、liquo-riceさん、Mike0121さん、レビューを有難うございました!

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.

4 participants