Skip to content

Commit e909f5f

Browse files
authored
Create 33-Search-In-Rotated-Sorted-Array.py
1 parent f27bbc9 commit e909f5f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

33-Search-In-Rotated-Sorted-Array.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def search(self, nums: List[int], target: int) -> int:
3+
l, r = 0, len(nums) - 1
4+
5+
while l <= r:
6+
mid = (l + r) // 2
7+
if target == nums[mid]:
8+
return mid
9+
10+
# left sorted portion
11+
if nums[l] <= nums[mid]:
12+
if target > nums[mid] or target < nums[l]:
13+
l = mid + 1
14+
else:
15+
r = mid - 1
16+
# right sorted portion
17+
else:
18+
if target < nums[mid] or target > nums[r]:
19+
r = mid - 1
20+
else:
21+
l = mid + 1
22+
return -1

0 commit comments

Comments
 (0)