-
Notifications
You must be signed in to change notification settings - Fork 0
Create First Unique Character in a String.md #5
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
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
スライスはコピーされるので、他の解き方よりは計算量が多くなっているはずです。
ryosuketc/leetcode_arai60#11 (comment)
char は s の文字列を左から見ているので、if 文でも s[:i] が左側にある方が個人的には自然な気がしました。趣味の範囲と思います。
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.
スライスのコピーの認識はありませんでした。ありがとうございます。sの見方も左からの方が自然ですね。
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.
str.find のところを読んだことがありますか?
https://docs.python.org/3/library/stdtypes.html#str.find
str.find(sub[, start[, end]])
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://github.com/rinost081/LeetCode/pull/14 | ||
https://github.com/Satorien/LeetCode/pull/15/ | ||
|
||
- Counterメソッドを使うと文字の出現回数を辞書のように保持できて便利 |
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://docs.python.org/3/library/collections.html#collections.Counter
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: | ||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね。一応、この問題は LRU を連想してもいいかなと思います。
このあたりをなんとなく見ておいてもらえると嬉しいです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.hztxgiufh8yd
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.
LRUというキャッシュ管理アルゴリズムなるものが存在するのですね.勉強になります.ありがとうございます.
for char in s: | ||
char_to_count[char] += 1 | ||
for i, char in enumerate(s): | ||
if char_to_count[char]==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.
読みやすいです。細かいですがここは" == "でしょうか。
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.
仰るとおりですね.
problem:
https://leetcode.com/problems/first-unique-character-in-a-string/description/