Open
Conversation
Fuminiton
reviewed
Jul 22, 2025
| else: | ||
| right = middle - 1 | ||
| return -1 | ||
| ``` |
There was a problem hiding this comment.
いいと思います。
が、個人的には回転位置を見つけたのちにソート済み配列(先頭から回転位置 or 回転位置から末尾)を探索する方針の方が理解しやすいかと思います
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます。書いてみました。わかりやすい方針だと思いました。
import bisect
class Solution:
def search(self, nums: List[int], target: int) -> int:
min_index = self._find_min_index(nums)
if target > nums[-1]:
i = bisect.bisect_left(nums[:min_index], target)
if i < min_index and nums[i] == target:
return i
else:
i = bisect.bisect_left(nums[min_index:], target) + min_index
if i < len(nums) and nums[i] == target:
return i
return -1
def _find_min_index(self, nums: List[int]) -> int:
return bisect.bisect_left(nums, True, key=lambda x: x <= nums[-1])
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.
This Problem
33. Search in Rotated Sorted Array
Next Ploblem
1011. Capacity To Ship Packages Within D Days
言語: Python