diff --git a/python/0081-search-in-rotated-sorted-array-II.py b/python/0081-search-in-rotated-sorted-array-II.py new file mode 100644 index 000000000..2109dfc4c --- /dev/null +++ b/python/0081-search-in-rotated-sorted-array-II.py @@ -0,0 +1,23 @@ +class Solution: + def search(self, nums: List[int], target: int) -> bool: + left,right = 0,len(nums) - 1 + while left <= right: + mid = left + (right - left) // 2 + if nums[mid] == target: + return True + + #Left sorted portion + if nums[left] < nums[mid]: + if nums[left] <= target < nums[mid]: + right = mid - 1 + else: + left = mid + 1 + #Right sorted portion + elif nums[left] > nums[mid]: + if nums[mid] < target <= nums[right]: + left = mid + 1 + else: + right = mid - 1 + else: + left += 1 + return False \ No newline at end of file