Skip to content

Commit 300f9bf

Browse files
committed
feat: add search rotated sorted array recursive solution
1 parent 32f2c2f commit 300f9bf

File tree

2 files changed

+20
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)