-
Notifications
You must be signed in to change notification settings - Fork 0
387. First Unique Character in a String #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ささいなことですが 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ恥ずかしいです😇
確かに検索したら、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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
本当は平衡木を使う方法もあるんですが、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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ、常に self.sentinel.prev であるという理解でいいですか? (条件分岐を減らせますか?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにその方が素直かつ分岐も減らせますね。ありがとうございます
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
とてもきれいで良いなと思いました。
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.