Skip to content

Conversation

@jiku0730
Copy link
Owner

@jiku0730 jiku0730 commented Jun 2, 2025

No description provided.

Copy link

@YUTA-Uchi YUTA-Uchi left a comment

Choose a reason for hiding this comment

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

ぱっと見た感じのレビューになってしまいましたが、レビューしてみました。
ちなみに他の方がやっているようにプルリクエストを作成する際に
問題:https://leetcode.com/problems/linked-list-cycle-ii/
次やる問題:○○
などと書くと親切です。
あとはやり方に迷ったらdiscord内を検索したり、
https://docs.google.com/document/d/1bjbOSs-Ac0G_cjVzJ2Qd8URoU_0BNirZ8utS3CUAeLE/edit?tab=t.0#heading=h.yu0571z9y1kx
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.gp4hkfr3qfqc
あたりを参考にすると良いです!

// 後半側に重複がある
return findDuplicate(nums + numsSize / 2, numsSize - numsSize / 2);
}
}

Choose a reason for hiding this comment

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

再帰の場合に気になるのはどんな条件の時に終了して再帰を抜けてくか、です。ぱっとこのコードを見たときにもこれ、再帰の終了条件はどこだ?と迷いました。

{
int i;
int first;
static int numSize_copy = numsSize;

Choose a reason for hiding this comment

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

まだstatic変数を使う意図をつかめていないのですが、static変数は普通の変数とは違い静的領域に場所が確保されます。なのでそれを再帰で使う場合、何度も何度も同じ関数を再帰している間に、その再帰全体から自由にアクセスできるある種のグローバル変数がほしいって感じでしょうか?

static int numSize_copy = numsSize;

i = 0;
first = 0;

Choose a reason for hiding this comment

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

firstという命名の意図が若干分かりづらい気はします。

## Comments
09:32
空間計算量がO(1)であることがめっちゃだるそう。配列の値をそのまま使う。。。?
2分探索ならうまくいくのかなぁ。

Choose a reason for hiding this comment

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

この問題を見て真っ先に二分探索を着想するのはすごいですね!自分は思いつかなかったです。

}
```
うごかんなぁ。
セグフォがでる。
Copy link

Choose a reason for hiding this comment

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

再帰したときに nums が変わっているので、numsSize_copy までループを回しても動きませんね。

}

```
めっちゃきれいに実装できた。
Copy link

Choose a reason for hiding this comment

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

はい、このコードを、自然言語で説明してくれませんか。
そして、コードとその自然言語の「距離」が十分近ければ、読みやすいコードであると私は思います。

行けてそう!!
![result](image.png)
いけた!!
なんかめちゃ早の人たちいる。
Copy link

Choose a reason for hiding this comment

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

フロイドのうさぎとかめといわれるアルゴリズムを応用した方法がありますね。
いや、正直、これ見たことあるんですが、別にそこができても点数がつかないタイプの問題だと思います。

この配列を「ゲームブック」だと思いましょう。
{1, 2, 4, 5, 3, 2}
これは、0ページを開くと「1ページに行け」と書いてあり、4ページを開くと「3ページに行け」と書いてあった、と解釈します。

まず、0ページ目に戻ってくることはありません。
次に、このゲームブックは終わりません。どのページもどこかに行くからです。
そして、ゲームブックは有限です。

そうすると、0から開始して、このゲームブックを進めていくと、あるところから循環が始まるということです。その循環の開始点は0ではないことから、「(循環開始の)ページに行け」と書かれたページが少なくとも2つあったことを示しています。

これはフロイドのアルゴリズムで解けます。

Copy link

Choose a reason for hiding this comment

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

@jiku0730 jiku0730 changed the title 287 find the duplicate number 287. find the duplicate number Jun 3, 2025
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.

4 participants