upd
This commit is contained in:
22
algorithms/labs/lab8/code/search/2.py
Normal file
22
algorithms/labs/lab8/code/search/2.py
Normal file
@@ -0,0 +1,22 @@
|
||||
def search_rotated(nums, target):
|
||||
l, r = 0, len(nums) - 1
|
||||
while l <= r:
|
||||
m = (l + r) // 2
|
||||
if nums[m] == target:
|
||||
return m
|
||||
if nums[m] >= nums[l]:
|
||||
if nums[l] <= target < nums[m]:
|
||||
r = m - 1
|
||||
else:
|
||||
l = m + 1
|
||||
else:
|
||||
if nums[m] < target <= nums[r]:
|
||||
l = m + 1
|
||||
else:
|
||||
r = m - 1
|
||||
return -1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(search_rotated([4, 5, 6, 7, 0, 1, 2], 0))
|
||||
print(search_rotated([4, 5, 6, 7, 0, 1, 2], 3))
|
||||
Reference in New Issue
Block a user