-
Notifications
You must be signed in to change notification settings - Fork 0
First Unique Character in a String.md #14
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
Open
katataku
wants to merge
1
commit into
main
Choose a base branch
from
387.-First-Unique-Character-in-a-String
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
URL: https://leetcode.com/problems/kth-largest-element-in-a-stream/description/ | ||
|
||
# Step 1 | ||
|
||
- 実装時間: 10分 | ||
- len(s)をnとして | ||
- 時間計算量: O(n) | ||
- 空間計算量: O(n) | ||
|
||
```python | ||
class Solution: | ||
def firstUniqChar(self, s: str) -> int: | ||
char_to_count = Counter(s) | ||
for i, char in enumerate(s): | ||
if char_to_count[char] == 1: | ||
return i | ||
return -1 | ||
``` | ||
|
||
- characterというより、C言語のchar型のイメージであえて英単語の省略形を選んだ | ||
|
||
# Step 2 | ||
|
||
- 参考にしたURL | ||
- https://github.com/Exzrgs/LeetCode/pull/9 | ||
- https://github.com/SuperHotDogCat/coding-interview/pull/22 | ||
- https://github.com/hayashi-ay/leetcode/pull/28/files | ||
- https://github.com/su33331/practice/pull/2 | ||
- https://github.com/fhiyo/leetcode/pull/18 | ||
- https://github.com/TORUS0818/leetcode/pull/17 | ||
- https://github.com/nittoco/leetcode/pull/20 | ||
- https://github.com/kazukiii/leetcode/pull/16 | ||
- https://github.com/Mike0121/LeetCode/pull/32 | ||
- https://github.com/Yoshiki-Iwasa/Arai60/pull/14 | ||
- https://github.com/seal-azarashi/leetcode/pull/15 | ||
- https://github.com/hroc135/leetcode/pull/15 | ||
- https://github.com/ryoooooory/LeetCode/pull/20 | ||
- https://github.com/tarinaihitori/leetcode/pull/15 | ||
- https://github.com/philip82148/leetcode-arai60/pull/4 | ||
- https://github.com/colorbox/leetcode/pull/29 | ||
- https://github.com/Hurukawa2121/leetcode/pull/15 | ||
|
||
- 追加質問でありそうだなと思ったやつ | ||
- Counterを使わずに書くと? | ||
- 自分で辞書を作る | ||
- その場合、defaultdictを使わずに書くと? | ||
- 26文字char配列を使う方法 | ||
- 辞書を作らないのであれば、左右から検索して最初に見つかった位置が一致していればunique, という探し方もできる。 | ||
|
||
```python | ||
class Solution: | ||
def firstUniqChar(self, s: str) -> int: | ||
for c in s: | ||
if s.rindex(c) == s.index(c): | ||
return s.index(c) | ||
return -1 | ||
``` | ||
|
||
- > `return next(iter(char_to_first_appear_index.values()))` | ||
- 一瞬、「リストにして`[0]`でいいじゃん?」と思ったけど、リストを作らずにiteratorのまま次の要素だけ持ってきているので効率的。 | ||
- 見たことない書き方だったけど、効率的な書き方だと思って感動。 | ||
- https://github.com/hayashi-ay/leetcode/pull/28/files | ||
|
||
- > c は、私は許容です。char は、C/C++/Java などで予約語なので、私は避けます。 | ||
- 僕がstep1を解いてる時も、「文字コードから文字に変換するbuilt-inの名前はchrだっけcharだっけ。それと被らないようにしないと。」と、一度考えてた。これは脳のリソースを使ってるということ。 | ||
- https://github.com/su33331/practice/pull/2/files#r1615489250 | ||
|
||
- > -1 がどのような意味を持つのかコードを見るだけだと分からないので、定数にして役割に準じた名前をつけたり、コードコメントを残すといったことをした方がいいように思いました。 | ||
- https://github.com/Exzrgs/LeetCode/pull/9/files#r1703858740 | ||
- > `return -1; // Not Found` | ||
- https://github.com/colorbox/leetcode/pull/29/files#r1861038507 | ||
|
||
- grueやbleenのはなし | ||
- https://github.com/fhiyo/leetcode/pull/18/files#r1629654317 | ||
|
||
```python | ||
class Solution: | ||
def firstUniqChar(self, s: str) -> int: | ||
char_to_frequency = Counter(s) | ||
for i, c in enumerate(s): | ||
if char_to_frequency[c] == 1: | ||
return i | ||
return -1 # Not found | ||
``` | ||
|
||
# Step 3 | ||
|
||
- len(s)をnとして | ||
- 時間計算量: O(n) | ||
- 空間計算量: O(n) | ||
|
||
```python | ||
class Solution: | ||
def firstUniqChar(self, s: str) -> int: | ||
char_to_frequency = Counter(s) | ||
for i, c in enumerate(s): | ||
if char_to_frequency[c] == 1: | ||
return i | ||
return -1 # Not found | ||
``` |
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.
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.
余力があれば Counter の内部実装を読むといいと思うのと、LinkedHashMap のようなものを使う実装は見ておくといいでしょう。