-
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
Open
potrue
wants to merge
1
commit into
main
Choose a base branch
from
35
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,45 @@ | ||
| ## 何も見ずに解いてみる | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int searchInsert(vector<int>& nums, int target) { | ||
| int left = 0; | ||
| int right = nums.size(); | ||
| while (left < right) { | ||
| int mid = left + (right - left) / 2; | ||
| if (target > nums[mid]) left = mid + 1; | ||
| if (target <= nums[mid]) right = mid; | ||
| } | ||
| return left; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## 他の人のコードを見てみる | ||
|
|
||
| https://github.com/tokuhirat/LeetCode/pull/41/files | ||
| https://github.com/skypenguins/coding-practice/pull/12/files | ||
| https://github.com/ryosuketc/leetcode_arai60/pull/30/files | ||
| https://github.com/Fuminiton/LeetCode/pull/41/files | ||
| https://github.com/takuya576/leetcode/pull/1/files | ||
| https://github.com/Ryotaro25/leetcode_first60/pull/45/files | ||
|
|
||
| いろいろなバリエーションで頭の中のシミュレーションを回してみようとすると難しい。。。 | ||
| とりあえず「答えとなる数字の下限と上限を閉区間または開区間でとらえる」と「leftとrightがずっと同じままループが回らない」ことだけ意識しておくことにしました。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int searchInsert(vector<int>& nums, int target) { | ||
| int left = 0; | ||
| int right = nums.size(); | ||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. ぶら下がりif文はメンテナンスしていった際に事故が起こりやすいという観点から、避けられるようです。 あと、短い処理なのでelseで書いてもわかりやすい気がします。 |
||
| } | ||
| return left; | ||
| } | ||
| }; | ||
|
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. 良いと思います。 |
||
| ``` | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
念のため確認させてください。今回の二分探索を書くとき、丸暗記したパターンを書き出しましたか?それとも、二分探索の仕組みを理解し、設定を考え、設定をもとにコードを書きましたか?
もし前者ですと、自分が使う道具を理解しようとしていないという、バッドシグナルだと思います。
試しに以下に答えてみてください。
int mid = (left + right) / 2;のほうがシンプルですが、なぜint mid = left + (right - left) / 2;なのですか?left = mid + 1;の+ 1の部分は、なぜ+ 1なのですか?- 1や0でないのはなぜですか?right = mid;にはなぜ- 1や+ 1を付けていないのですか?leftを return しているのですか?Uh oh!
There was an error while loading. Please reload this page.
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.
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.
ありがとうございます。理想的な理解の仕方の一つをしていると思います。