请实现有重复数字的升序数组的二分查找
给定一个 元素有序的(升序)长度为n的整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的第一个出现的target,如果目标值存在返回下标,否则返回 -1
数据范围:
进阶:时间复杂度,空间复杂度
[1,2,4,4,5],4
2
从左到右,查找到第1个为4的,下标为2,返回2
[1,2,4,4,5],3
-1
[1,1,1,1,1],1
0
class Solution: def search(self , nums: List[int], target: int) -> int: lo,hi = 0, len(nums)-1 while lo <= hi: mid = lo + (hi-lo) //2 if nums[mid] == target: while mid != 0 and nums[mid-1] == nums[mid]: mid -= 1 return mid elif nums[mid] > target: hi = mid -1 else: lo = mid +1 return -1
class Solution: def search(self , nums: List[int], target: int) -> int: # write code here l, r = 0, len(nums)-1 while l <= r: i = (l+r)//2 while target == nums[i]: if i -1 < 0&nbs***bsp;target != nums[i-1]: return i else: i -= 1 if target > nums[i]: l = i+1 elif target < nums[i]: r = i-1 return -1
class Solution: def search(self , nums: List[int], target: int) -> int: # write code here n=len(nums) left,right=0,n-1 res=-1 while left<=right: mid=(left+right)//2 if nums[mid]==target: res=mid right=mid-1 elif nums[mid]>target: right=mid-1 elif nums[mid]<target: left=mid+1 return res
class Solution: def search(self , nums: List[int], target: int) -> int: # write code here if not nums: return -1 left=0 right=len(nums)-1 while left<right: mid=(left+right)//2 if nums[mid]<target: left=mid+1 elif nums[mid]>target: right=mid-1 elif nums[mid]==target: right-=1 return left if nums[left]==target else -1
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 如果目标值存在返回下标,否则返回 -1 # @param nums int整型一维数组 # @param target int整型 # @return int整型 # class Solution: def search(self , nums: List[int], target: int) -> int: # write code here front,end = 0,len(nums) - 1 while front <= end: temp = (front + end) // 2 if nums[temp] == target: temp1 = temp while temp1 >= 0 and nums[temp1] == target: temp1 -= 1 return temp1+ 1 elif nums[temp] > target: end = temp - 1 else: front = temp + 1 else: return -1
class Solution: def search(self , nums: List[int], target: int) -> int: left,right = 0,len(nums)-1 while left<=right: mid = (left+right)//2 if nums[mid]<target: left = mid +1 elif nums[mid]>target: right = mid -1 elif nums[mid]==target: right = mid -1 if left < len(nums) and nums[left] == target: return left else: return -1
class Solution: def search(self , nums , target ): # write code here n = len(nums) if not n:return -1 left,right = 0,n-1 while left <= right: mid = left+(right-left)//2 if nums[mid] < target: left = mid+1 elif nums[mid] >target: right = mid-1 else: left = mid while left > 0: if nums[left-1] != target: break left -= 1 return left return -1
class Solution: def search(self , nums , target ): # write code here res = -1 low, high = 0, len(nums)-1 while low <= high: mid = low + ((high - low) >> 1) if nums[mid] == target: res = mid high = mid - 1 elif nums[mid] > target: high = mid - 1 else: low = mid + 1 return res
class Solution: def search(self , nums , target ): # write code here if not nums: return -1 start = 0 end = len(nums) while start <= end: mid = (end+start) // 2 if nums[mid] == target: if mid>0 and nums[mid-1] == target: end = mid - 1 else: return mid elif nums[mid] < target: start = mid + 1 else: end = mid - 1 return -1
class Solution: def search(self , nums , target ): if len(nums) == 0: return -1 left = 0 right = len(nums)-1 t = int((left+right)/2) while left <= right: if nums[t] == target: while nums[t] == target and t>=0: t = t-1 return t+1 elif target < nums[t]: right = t-1 elif target > nums[t]: left = t+1 t = int((left+right)/2) return -1
class Solution: def search(self , nums , target ): # write code here n = len(nums) if n==0: return -1 left, right = 0, n-1 while left<right: mid = left + (right-left)//2 if nums[mid]==target: while mid-1>=0 and nums[mid-1]==nums[mid]: mid-=1 return mid elif nums[mid]<target: left=mid+1 else: right = mid if nums[left]==target: while left-1>=0 and nums[left-1]==nums[left]: left-=1 return left else: return -1