Skip to content

141. linked list cycle#1

Merged
5ky7 merged 6 commits into
mainfrom
141.-Linked-List-Cycle
Mar 3, 2025
Merged

141. linked list cycle#1
5ky7 merged 6 commits into
mainfrom
141.-Linked-List-Cycle

Conversation

@5ky7
Copy link
Copy Markdown
Owner

@5ky7 5ky7 commented Mar 3, 2025

@5ky7 5ky7 merged commit 54be12d into main Mar 3, 2025
fast = fast->next->next;
slow = slow->next;

if(fast == slow) return true;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前にぶら下がりについての議論があったので参考になるかもしれません
philip82148/leetcode-swejp#9 (comment)
https://discord.com/channels/1084280443945353267/1313503038358552607/1315031115014082600

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます.
ぶら下がり文という名前があることを今知りました.参考になります.
教えていただいた資料を読んで気づきましたが,style guide的に私の書き方はifの後にスペースがないからbadですね.気をつけることとします.

Copy link
Copy Markdown

@fuga-98 fuga-98 Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

リスクがあるので個人的には避けたほうがよいと思ってます

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

条件1行,中身1行でも避けた方が安心ということですね.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2014年には、goto failバグというものが見つかっています。
括弧でくくっていれば、防げていたかもしれませんね。
https://qiita.com/tomohisaota/items/e6995e89b843e1295c08
https://www.sigbus.info/compilerbook 「コラム: インデントのエラーで生じたセキュリティバグ」

# step1
- 構造体・ポインタの知識がなかったので,[これ](https://atcoder.jp/contests/apg4b)で学習.
- その上でリストの成分からなるvectorを用意して,p.nextがこのvector内のどれかを指していたらtrue,指す前にp.nextがnullに到達したらfalseを出力という方針で書いてみる.
- が,うまくいかず20分ほど経ったので回答を見る.鬼ごっこでやるのか,と感動.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

鬼ごっこの解き方よりもsetを用いた解き方のほうが重要なようです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.2k4z0wt6ytf9

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます.
setを用いる方法は自然な発想で参考になります.
setでなくvectorを持ち出そうとした時点でデータ構造の基礎が抜けているようでした.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set に含まれるかのチェックは C++ でさまざま用意されているようです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます.
containsはじめて知りました.勉強になります.

# step1
- 構造体・ポインタの知識がなかったので,[これ](https://atcoder.jp/contests/apg4b)で学習.
- その上でリストの成分からなるvectorを用意して,p.nextがこのvector内のどれかを指していたらtrue,指す前にp.nextがnullに到達したらfalseを出力という方針で書いてみる.
- が,うまくいかず20分ほど経ったので回答を見る.鬼ごっこでやるのか,と感動.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set に含まれるかのチェックは C++ でさまざま用意されているようです。

}
return false;
}
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while や if の間にスペースを入れるとより良いと思います。

if (condition) { // Good - no spaces inside parentheses, space before brace.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます.
ところで私は実務の経験がないのでこういった慣習に疎いです.可読性のためにもなるだけ早く当たり前のものにしておきたいのですが,プログラマーの方はスタイルガイドを通読して身につけるのでしょうか?あるいは書いていて指摘を受けては直していくといった感じでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他の方のコードを見たり、書き方を迷ったら規範や同僚の書き方を見て真似るなどを繰り返して身につけていくことなのかと思っています。講師陣の以下の見解が参考になります。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良い資料をありがとうございます!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

逆にスタイルを守らないものの極致として IOCCC というコンテストがありますね。
https://www.ioccc.org/

あとは、これが C++ readability team にいた Omoikane さんのコードです。
https://uguu.org/sources.html

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2次イラストサイトが出てきたと思ってリンク間違えたかな?と思ったらまさかのコードによるAAとは...
面白いサイトたちをありがとうございます.

@myzn0806
Copy link
Copy Markdown

myzn0806 commented Mar 4, 2025

良いと思いました!
他の人のPRも参考にして、フロイドのアルゴリズムだけでなくsetを使った実装もしてみると良いと思います。
(自分が見つけられたのはこのPRでした https://github.com/momeemt/LeetCode/pull/1/files)
ほかにもdiscordを漁ればたくさんあると思います。

ListNode *fast = head;
ListNode *slow = head;

while(fast != nullptr && fast->next != nullptr){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ポインターの nullptr チェックは != nullptr 書かない人もいますね。

- 短いしいけるやろ,と思ったものの"!="を"=="と書いてしまいWA.
- その後3回Acceptedを出して終了
- step2で他の人の回答を読むのを忘れていた.
- レビューは慣れてからで良かったみたい.とりあえずは読むことにしよう
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

読むのがわりと本体です。コメント集に大体載ってます。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.2k4z0wt6ytf9

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他人のコードやそれに対するレビューを読む習慣をつけていきます.
コメント集もstep3の前には読んでいきます.ありがとうございます..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants