Conversation
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
| - torusさんへのodaさんのコメントを見る限り、十分高速ではあるらしい | ||
| - [コード](https://github.com/python/cpython/blob/main/Python/bltinmodule.c#L1796C1-L1796C8) も流し読み やはり全要素をなめてる | ||
| - next iterを使えば2回ループは避けれる(無理矢理感) | ||
| - secoundは微妙な気がしてきた。seenとかduplicatedの方が良かったかも。 |
There was a problem hiding this comment.
ささいなことですが secound のところ、2nd の意味であれば second だと思います。
https://dictionary.cambridge.org/dictionary/english/second
また、duplicate には名詞の用法もあるようなので、変数名を duplicates と名詞形にするのもよさそうだと思いました。
https://dictionary.cambridge.org/dictionary/english/duplicate
There was a problem hiding this comment.
これ恥ずかしいです😇
確かに検索したら、duplicated_nameみたいに後に名詞が来る場合が多いですね。ありがとうございます
https://source.chromium.org/search?q=duplicated&ss=chromium%2Fchromium%2Fsrc&start=21
| ## Step2 | ||
|
|
||
| - https://github.com/shining-ai/leetcode/pull/15 | ||
| - LinkedHashMapが、多分Step1の最後で言ったようなことができるデータ構造。後で、LinkedHashMapの実装をしてみる |
There was a problem hiding this comment.
本当は平衡木を使う方法もあるんですが、Python だと標準にはないようですね。
Ryotaro25/leetcode_first60#16 (comment)
| self.sentinel = LinkedListDictNode() | ||
| self.sentinel.prev = self.sentinel | ||
| self.sentinel.next = self.sentinel | ||
| self.last = self.sentinel |
There was a problem hiding this comment.
これ、常に self.sentinel.prev であるという理解でいいですか? (条件分岐を減らせますか?)
There was a problem hiding this comment.
確かにその方が素直かつ分岐も減らせますね。ありがとうございます
| class Solution: | ||
| def firstUniqChar(self, s: str) -> int: | ||
| char_to_freq = defaultdict(int) | ||
| for c in s: | ||
| char_to_freq[c] += 1 | ||
| for i, c in enumerate(s): | ||
| if char_to_freq[c] == 1: | ||
| return i | ||
| return -1 |
https://leetcode.com/problems/first-unique-character-in-a-string/description/
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.