Open
Conversation
potrue
reviewed
Mar 2, 2026
|
|
||
| // 閉区間の場合、mid が begin と一致すると区間が縮まらない | ||
| if (target <= nums[mid]) { | ||
| answer = mid; // true の頭を取る |
There was a problem hiding this comment.
ここでif (begin == end) {return end + 1}としてしまってもいいと思いました。
potrue
reviewed
Mar 2, 2026
| int begin = 0; | ||
| int end = nums.size() - 1; | ||
|
|
||
| int answer = nums.size(); |
There was a problem hiding this comment.
良い代替案が思いつくわけではないのですが、answerという名前の変数が頻繁に変わるのは少し違和感があるかもしれません。
nodchip
reviewed
Mar 3, 2026
| } | ||
| } | ||
|
|
||
| // 7. 最後はどっちでも良い |
There was a problem hiding this comment.
end の位置には target より小さい値があるため、 begin でないとまずいように思いました。
mamo3gr
reviewed
Mar 10, 2026
|
|
||
| ## Step 1 | ||
|
|
||
| 時間計算量 $O(N \log N)$ の制約があるので二分探索でやることが求められているんだろう。`step1.cpp` に書いた。 |
There was a problem hiding this comment.
[nits]
Suggested change
| 時間計算量 $O(N \log N)$ の制約があるので二分探索でやることが求められているんだろう。`step1.cpp` に書いた。 | |
| 時間計算量 $O(\log N)$ の制約があるので二分探索でやることが求められているんだろう。`step1.cpp` に書いた。 |
mamo3gr
reviewed
Mar 10, 2026
| // 閉区間の場合、mid が begin と一致すると区間が縮まらない | ||
| if (target <= nums[mid]) { | ||
| answer = mid; // true の頭を取る | ||
| end = mid - 1; // さらに左側を探索 |
There was a problem hiding this comment.
end より右は常にtrue (nums[i] >= target) ということですね。
mamo3gr
reviewed
Mar 10, 2026
| answer = mid; // true の頭を取る | ||
| end = mid - 1; // さらに左側を探索 | ||
| } else { | ||
| begin = mid + 1; // false 側を捨てる |
mamo3gr
reviewed
Mar 10, 2026
|
|
||
| int answer = nums.size(); | ||
|
|
||
| // 一意に定まったら終了、つまり begin > end となったら終了 |
There was a problem hiding this comment.
答えが [begin, end] の中にある状態を保つ
なのに
begin > end となったら終了
というのは違和感がありました。言葉通り捉えると、解が存在しないように思います。
|
探索の3パターンを書き分けてみるとより理解が深まると思います。 #include <vector>
class Solution {
public:
int searchInsert(std::vector<int>& nums, int target) {
int ng = -1;
int ok = nums.size();
while (ok - ng > 1) {
int mid = ng + (ok - ng) / 2;
if (nums[mid] >= target) {
ok = mid;
} else {
ng = mid;
}
}
return ok;
}
}; |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題
https://leetcode.com/problems/search-insert-position/
次の問題
153. Find Minimum in Rotated Sorted Array