重复元素会影响到运行。
method 1:brute
class Solution:
def search(self, nums: List[int], target: int) -> bool:
for i in nums:
if i == target:
return True
return False
method 2:binary
class Solution:
def search(self, nums: List[int], target: int) -> bool:
def binary(arr, target):
lo, hi = 0, len(arr) - 1
while lo <= hi:
mid = (lo + hi) // 2
if arr[mid] == target:
return True
elif arr[mid] > target:
hi -= 1
else:
lo += 1
return False
if nums[0] == target: return True
for i in range(1, len(nums)):
if nums[i] == target:
return True
if nums[i] < nums[i-1]:
return binary(nums[i:], target)
return False