Open
Conversation
oda
reviewed
Mar 14, 2026
| - 計算量はO(nl) | ||
| - 入力がアルファベット小文字以外のときには例外処理で対応 | ||
| - 実行時間はsol1の方が速い | ||
| - pythonのsortedが高速でloglが定数倍より速い? |
There was a problem hiding this comment.
Native code の部分だというだけで感覚10-20倍程度速いです。
下につけたのは適当に拾ってきたソートの速度の比較ですが、ネイティブコードにすると10-20倍くらいは簡単に変わるんですね。
C 言語と Python は50-100倍くらい違い、ただしネイティブコードでもガーベージコレクションなど Python Object の操作をしないといけないので、C 言語並までにはならないだろうくらいの推測です。
https://discord.com/channels/1084280443945353267/1367399154200088626/1372840818397810701
Owner
Author
There was a problem hiding this comment.
なるほど、そう考えると色々な組み込み関数は知っておくと良さそうです。(基数ソートをRadix sortというの知らなかったです。これが一番速いんですね)
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| anagram_sets = defaultdict(list) | ||
| for orig_str in strs: | ||
| sorted_str = sorted(orig_str) |
There was a problem hiding this comment.
私は "".join まで上の行でしてしまうかもしれません。まあ、どちらでもいいです。
Owner
Author
There was a problem hiding this comment.
sorted_key = "".join(sorted(orig_str))
anagram_sets[sorted_key].append(orig_str)この方が見やすいですね
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/group-anagrams/