Let's take this example
Example 1
class Solution:
def search(self, nums: List[int], target: int) -> int:
low, high = 0, len(nums) - 1
while low <= high:
midpoint = (low + high) // 2
if nums[midpoint] == target:
return midpoint
if nums[low] < nums[midpoint]:
if nums[low] <= target < nums[midpoint]:
high = midpoint - 1
else:
low = midpoint + 1
else:
if nums[midpoint] < target <= nums[high]:
low = midpoint + 1
else:
high = midpoint - 1
return -1
Example 2
class Solution:
def search(self, nums: List[int], target: int) -> int:
low, high = 0, len(nums) - 1
while low <= high:
midpoint = (low + high) // 2
if nums[midpoint] == target:
return midpoint
if nums[low] < nums[midpoint]:
if nums[low] <= target < nums[midpoint]:
high = midpoint - 1
else:
low = midpoint + 1
else:
if nums[midpoint] < target and nums[high] >= target:
low = midpoint + 1
else:
high = midpoint - 1
return -1
With this input
nums =
[3,1]
target =
1
You would expect the same. Running them though, the results are different.