Open
Conversation
naoto-iwase
reviewed
Feb 3, 2026
Comment on lines
+24
to
+25
| # target <= nums[mid] だから、midまでしか詰められなくない?と思ってしまう | ||
| last = mid - 1 |
There was a problem hiding this comment.
docstringに閉区間と書かれており、last = len(nums) - 1という初期化なので、lastより右側のインデックスiに対してtarget <= nums[i]だと(lastのことを)捉えていることになります。
そうなると、i = midはlastより右にいないといけないのでlast = mid - 1という更新になります。
garunitule
reviewed
Feb 5, 2026
|
|
||
| ```java | ||
| class Solution { | ||
| public int searchInsert(int[] nums, int target) { |
| ### 他の人のコード(つづき) | ||
|
|
||
| 書くより読むのが大事とのことで、読みながら注釈をつけていく。 | ||
| できる人は頭の中でできそうだが、自分は書きながらでないとできない。 |
There was a problem hiding this comment.
自分は最初全然できなかったです
ただ、他の人のコードを読んだり、他の二分探索の問題を不変条件を意識しながら解いたら、ある程度できるようになりました
35_search-insert-position/memo.md
Outdated
| left_index = middle_index | ||
| else: | ||
| # target <= nums[middle_index] だから、 | ||
| # rightと以降右はtarget以上 |
There was a problem hiding this comment.
targetの時と等しい場合は早期returnがあるので、target以上ではなくtargetより大きいになりますね
どちらの解釈でもコードは変わらないし動くんですが、正確に言語化することが大事だと思ってます
Owner
Author
There was a problem hiding this comment.
ありがとうございます。以下で修正しました
https://github.com/mamo3gr/arai60/pull/39/changes/BASE..dc7d7523113941846336628d81725378416ebf26#r2778241496
どちらの解釈でもコードは変わらないし動くんですが、正確に言語化することが大事だと思ってます
これにとても同意です!
mamo3gr
commented
Feb 7, 2026
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/
学習方法
標準的な進め方と同様にしている。
Step 1
Step 2
Step 3
「10分以内に正解するコードが書ける」ことを確認する
(その後、レビュー依頼をする=本Pull Request)