Conversation
mamo3gr
reviewed
Jan 17, 2026
| - https://docs.python.org/ja/3/library/functions.html#next | ||
| - https://docs.python.org/ja/3/library/functions.html#iter | ||
| - こういうのを使っていると上級者なんだろうなと思う | ||
| - 自分から書く選択肢に全くないのでなんとかしたい気持ち |
There was a problem hiding this comment.
私も知らなくて、調べて見つけた記憶があります。
dict.values() の一番目だけ欲しいけど、dict[1] とは書けないし、リストにしたら全要素コピーされて無駄だぞ…。何か方法があるんじゃないかな、と思って調べて突き当たりました。
私の経験からですが「何か方法があるんじゃないかな」は大抵の場合、方法があります。
mamo3gr
reviewed
Jan 17, 2026
| # @lc code=start | ||
| class Solution: | ||
| def firstUniqChar(self, s: str) -> int: | ||
| charactor_to_index = dict() |
There was a problem hiding this comment.
タイポしてますね。
Suggested change
| charactor_to_index = dict() | |
| character_to_index = dict() |
mamo3gr
reviewed
Jan 17, 2026
Comment on lines
+15
to
+17
| if charactor in charactor_to_index: | ||
| del charactor_to_index[charactor] | ||
| continue |
There was a problem hiding this comment.
頭から読んだとき、
- なぜ辞書でヒットしたら、そこからインデックスを消しちゃうんだ?
seen_charactersに含まれているかの確認は先にしなくていいのか?- 正解は、ユニークな文字だけ を辞書に保存したいから
seen_charactersには add しなくていいのか?- 実は、辞書に追加されているなら
seen_charactersにも入っている(これは後ろを読んで分かる)
- 実は、辞書に追加されているなら
のように、実際の動きを頭の中で頑張ってシミュレートする必要があり、短い行数のわりに大変に感じました。
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.
解く問題
First Unique Character In A String
次に解く問題
Subarray Sum Equals K