Conversation
| class Solution: | ||
| def firstUniqChar(self, s: str) -> int: | ||
| for i, char in enumerate(s): | ||
| if char not in s[i+1:] and char not in s[:i]: |
There was a problem hiding this comment.
スライスはコピーされるので、他の解き方よりは計算量が多くなっているはずです。
ryosuketc/leetcode_arai60#11 (comment)
char は s の文字列を左から見ているので、if 文でも s[:i] が左側にある方が個人的には自然な気がしました。趣味の範囲と思います。
There was a problem hiding this comment.
スライスのコピーの認識はありませんでした。ありがとうございます。sの見方も左からの方が自然ですね。
There was a problem hiding this comment.
str.find のところを読んだことがありますか?
https://docs.python.org/3/library/stdtypes.html#str.find
str.find(sub[, start[, end]])
| https://github.com/rinost081/LeetCode/pull/14 | ||
| https://github.com/Satorien/LeetCode/pull/15/ | ||
|
|
||
| - Counterメソッドを使うと文字の出現回数を辞書のように保持できて便利 |
There was a problem hiding this comment.
メソッドではなくクラスだと思います。
https://docs.python.org/3/library/collections.html#collections.Counter
There was a problem hiding this comment.
勘違いしていました。ご指摘ありがとうございます。
| class Solution: | ||
| def firstUniqChar(self, s: str) -> int: | ||
| for i, char in enumerate(s): | ||
| if char not in s[i+1:] and char not in s[:i]: |
There was a problem hiding this comment.
str.find のところを読んだことがありますか?
https://docs.python.org/3/library/stdtypes.html#str.find
str.find(sub[, start[, end]])
| return i | ||
| return -1 | ||
| ``` | ||
| Counterを使うほうが簡潔だし,処理速度も早い. |
There was a problem hiding this comment.
そうですね。一応、この問題は LRU を連想してもいいかなと思います。
このあたりをなんとなく見ておいてもらえると嬉しいです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.hztxgiufh8yd
There was a problem hiding this comment.
LRUというキャッシュ管理アルゴリズムなるものが存在するのですね.勉強になります.ありがとうございます.
| for char in s: | ||
| char_to_count[char] += 1 | ||
| for i, char in enumerate(s): | ||
| if char_to_count[char]==1: |
problem:
https://leetcode.com/problems/first-unique-character-in-a-string/description/