-
Notifications
You must be signed in to change notification settings - Fork 0
35. Search Insert Position #41
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
| } | ||
| return left; | ||
| } | ||
| }; |
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.
良いと思います。
| while (left < right) { | ||
| int mid = left + (right - left) / 2; | ||
| if (nums[mid] < target) left = mid + 1; | ||
| if (nums[mid] >= target) right = mid; |
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.
ぶら下がりif文はメンテナンスしていった際に事故が起こりやすいという観点から、避けられるようです。
Hurukawa2121/leetcode#5 (comment)
あと、短い処理なのでelseで書いてもわかりやすい気がします。
| https://github.com/takuya576/leetcode/pull/1/files | ||
| https://github.com/Ryotaro25/leetcode_first60/pull/45/files | ||
|
|
||
| いろいろなバリエーションで頭の中のシミュレーションを回してみようとすると難しい。。。 |
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.
念のため確認させてください。今回の二分探索を書くとき、丸暗記したパターンを書き出しましたか?それとも、二分探索の仕組みを理解し、設定を考え、設定をもとにコードを書きましたか?
もし前者ですと、自分が使う道具を理解しようとしていないという、バッドシグナルだと思います。
試しに以下に答えてみてください。
- 区間には何が含まれますか?
- left が指す対象は何ですか?
- right が指す対象は何ですか?
- ループの不変条件 (left < right) を決めるとき、どのように決めましたか?
int mid = (left + right) / 2;のほうがシンプルですが、なぜint mid = left + (right - left) / 2;なのですか?left = mid + 1;の+ 1の部分は、なぜ+ 1なのですか?- 1や0でないのはなぜですか?right = mid;にはなぜ- 1や+ 1を付けていないのですか?- なぜ
leftを return しているのですか?
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.
- 答えになりえるindexが含まれます。
- 答えになりえる最小のindex(境界を含む)のつもりでした。
- 答えになりえる最大のindex(境界を含む)のつもりでした。
- この問題は
0 <= x <= nums.size()の中に必ず出すべき答えが存在する問題なので、答えの候補が複数あるうちはループを回し、一つに絞られた段階で抜けるというイメージでした。left != rightでもよかったかもしれません。 int mid = (left + right) / 2;だと(left + right)を計算する段階でintのオーバーフローが発生する可能性があると思ったのでこのようにしました。nums[mid] < targetであるとき既にmidは答えにはなりえないので、+ 1を付けました。また、+ 1をつけないと、midを計算するときの/ 2の操作が切り捨てなので、leftもrightも変化せずにループが回り続けることがあると思いました。(具体的には、right == left + 1であるような時を思い浮かべました。)nums[mid] >= targetであることはmidが答えになるケースを否定しない(nums[mid] == targetである可能性がある)ので、- 1をつけませんでした。- ここにたどり着く時点ですでに
left == rightになっていると思うので、leftをreturnしてもrightをreturnしてもよいと思うのですが、一番簡潔だと思いleftをreturnしました。
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
left a comment
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.
LGTM です!
問題文:
https://leetcode.com/problems/search-insert-position/description/