-
Notifications
You must be signed in to change notification settings - Fork 0
141. Linked List Cycle #12
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # 141. Linked List Cycle | ||
|
|
||
| https://leetcode.com/problems/linked-list-cycle/ | ||
|
|
||
| ## Comments | ||
|
|
||
| ### step1 | ||
|
|
||
| * まあこれは何度も解いているので… | ||
| * https://github.com/ryosuketc/leetcode_arai60/pull/1/files | ||
| * `set` と `unordered_set`、今回別に order いらないんだけど最悪計算量が悪くなる可能瀬尾を考えると、どっちでもいいときのデフォルトはとりあえず `set` でいいのかなあという気がしている。 | ||
| * https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.rxnrgw1lj851 | ||
| * どっちでもいいのではという風潮 | ||
| * https://cpprefjp.github.io/reference/set/set.html | ||
| * `contains` C++20 からなので割と新しい。なんでこれがなかったんだろう。まあ `count` とかすればいいのはそうだけど。 | ||
| * `insert_range` なんてのも新しい。C++23 | ||
|
|
||
| ### step2 | ||
|
|
||
| * フロイドのうさぎとかめも一応書いてみた | ||
| * https://github.com/nktr-cp/leetcode/pull/2/files | ||
| * Python の `==` vs. `is` (値比較と、オブジェクト / メモリアドレス) の話 | ||
| * たぶん C++ `==` は値の比較。オブジェクトの比較はポインタを使う (ポインタの `==` はメモリアドレスの比較) | ||
| * より厳密には `operator==` の実装依存、という話で、型によって違うという話だと思うが、個別の型でどう実装されているかまではチェックしていない | ||
| * https://en.cppreference.com/w/cpp/language/operators.html | ||
| * Java のジェネリクスみたいな話なんだと思っているが、ジェネリクス使った実装はやったことがない | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Template はコンパイル時にソースコードを自動生成する技術で、Generics は型をつけておいて実行時に解決するものです。Java の Generics はキャストしているだけですね。 上の operator== は演算子のオーバーロードの話なのでまた別です。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど…これが根本的に違うものだというのは理解していませんでした。Template 、なんかマクロのような感じを受けますね (
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. しっかり内容を検証してはいないですが、軽く Gemini に聞いてみた内容を記録しておきます。 1. Template はコンパイル時にソースコードを自動生成する技術これは C++ の Template の特徴を正確に捉えています。
2. Java の Generics はキャストしているだけですねこれも、Java の Generics の実装方法(型消去)を指した、やや簡略化した表現ですが、本質を突いています。
3. Generics は型をつけておいて実行時に解決するものですこれは "Generics" という言葉を一般論として語っていますが、恐らくコメンテーターは C# の Generics のような実装を念頭に置いている可能性が高いです。 C++ (コンパイル時生成) と Java (型消去) の中間的な実装として、C# や .NET のジェネリクスがあります。
まとめこのコメントは、以下の 3 者の実装方法の違いを対比しています。
コードレビューの文脈では、「C++ の Template は Java のような実行時のキャスト処理(オーバーヘッド)を伴うものではなく、コンパイル時に解決される全く別物(より高速な仕組み)ですよ」という意図で発言されたものと推測されます。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 正しいことを言ってそうです。 |
||
| * TODO: 上記 cppreference のようなテンプレートを使った書き方とかはまだ勉強していない。テンプレートもいずれは勉強する | ||
|
|
||
|
|
||
| ### step3 | ||
|
|
||
| * 何度も解いているやつなので省略 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode(int x) : val(x), next(NULL) {} | ||
| * }; | ||
| */ | ||
|
|
||
| #include <set> | ||
|
|
||
| class Solution { | ||
| public: | ||
| bool hasCycle(ListNode *head) { | ||
| std::set<ListNode*> visited; | ||
| ListNode* node = head; | ||
| while (node != nullptr) { | ||
| if (visited.contains(node)) { | ||
| return true; | ||
| } | ||
| visited.insert(node); | ||
| node = node->next; | ||
| } | ||
| return false; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode(int x) : val(x), next(NULL) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| bool hasCycle(ListNode *head) { | ||
| ListNode* fast = head; | ||
| ListNode* slow = head; | ||
| while (fast != nullptr && fast->next != nullptr) { | ||
| slow = slow->next; | ||
| fast = fast->next->next; | ||
| if (fast == slow) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| }; |
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.
C++ STL は、哲学として「どのようなコンテナもできるだけ同じメソッドを用意する」という考えがあった(と理解しています)。
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0458r2.html
https://langdev.stackexchange.com/questions/3960/why-did-stdset-not-have-a-contains-function-until-c20
https://stackoverflow.com/questions/42532550/why-does-stdset-not-have-a-contains-member-function
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.
(basic_)stringにだけlengthがあるのは、containsなどと同じ背景なのかな、と想像しました(一般的なコンテナはsizeを持つ)。https://cpprefjp.github.io/reference/vector/vector.html
https://cpprefjp.github.io/reference/string/basic_string.html