Skip to content

Commit 32f2c2f

Browse files
committed
feat: add search rotated sorted array iteratively
1 parent f441299 commit 32f2c2f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

arrays_and_strings/search_rotated_sorted_array/__init__.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def search_rotated_sorted_array(nums: list[int], target: int) -> int:
2+
low = 0
3+
high = len(nums) - 1
4+
5+
while low <= high:
6+
mid = low + (high - low)//2
7+
if nums[mid] == target:
8+
return mid
9+
10+
if nums[low] <= nums[mid]:
11+
if nums[low] <= target and target <= nums[mid]:
12+
high = mid - 1
13+
else:
14+
low = mid + 1
15+
16+
else:
17+
if nums[mid] <= target and target <= nums[high]:
18+
low = mid + 1
19+
else:
20+
high = mid - 1
21+
return -1

0 commit comments

Comments
 (0)