Conversation
nodchip
reviewed
Dec 2, 2025
|
|
||
| ## step2 | ||
| Step1としては、 | ||
| 「左半分 or 右半分のどっちかは必ず昇順」 |
|
|
||
| if nums[mid] == target: | ||
| return mid | ||
| elif nums[mid] > nums[left]: |
There was a problem hiding this comment.
他の個所が < または <= で統一されているにもかかわらず、ここだけ > なのが気になりました。 < に統一するとよいと思います。
| if nums[mid] == target: | ||
| return mid | ||
| elif nums[mid] > nums[left]: | ||
| if nums[left] <= target < nums[mid]: |
There was a problem hiding this comment.
念のため確認させてください。 nums[left] <= target < nums[mid] の代わりに nums[left] <= target とした場合、どのような問題が起きますか?また、 target < nums[mid] とした場合、どのような問題が起きますか?
| @@ -0,0 +1,24 @@ | |||
| ## 解答解説を見た後に自力で書いたコード | |||
|
|
|||
| class Solution: | |||
There was a problem hiding this comment.
仮に自分がこの問題を解くとしたら、 nums[left] <= target < nums[mid] の部分の条件を頭の中で整理するのが難しいと感じるため、この条件文を避けようと考えると思います。避けるにあたり、最小値の左側の境界を見つけたあと、 target が左半分にあるか、右半分にあるかを確認し、それらの中で再度二分探索をするという、 2 段階のコードを書くと思います。
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-in-rotated-sorted-array/description/
問題文の概要
There is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is possibly left rotated at an unknown index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be left rotated by 3 indices and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
You must write an algorithm with O(log n) runtime complexity.
次に解く予定の問題
1011-capacity-to-ship-packages-within-D-days
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/