我正在努力解决Find First and Last Position of Element in Sorted Array - LeetCode
- 在排序数组中查找元素的第一个和最后一个位置
中等
给出一个以升序排列的整数
nums
数组,找到给定target
值的开始和结束位置。您的算法的运行时复杂度必须为 O (log n )。
如果在数组中未找到目标,则返回
[-1, -1]
。示例1:
Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4]
示例2:
Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1]
我的解决方案很简单bisect-leftmost
和bisect-rightmost
,然后将它们组合
#leftmost solution
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
left = self.search_leftmost(nums, target)
right = self.search_rightmost(nums, target)
if left != len(nums) and right != len(nums) and nums[left] == nums[right] == target:
return [left, right]
else: return [-1, -1]
def search_leftmost(self, nums, target) -> int:
lo = 0
hi = len(nums)
while lo < hi:
mid = (lo+hi) // 2
if target > nums[mid]:
lo = mid + 1
else:
hi = mid
return lo
def search_rightmost(self, nums, target) -> int:
lo = 0
hi = len(nums)
while lo < hi:
mid = (lo+hi) // 2
if target >= nums[mid]:
lo = mid + 1
else:
hi = mid
return lo -1
分两步完成工作,
如何将它们合并在一起并一口气解决问题。因为它们之间存在细微差异。
答案 0 :(得分:1)
为简化代码,您只能使用search_leftmost
,实际上search_rightmost(nums, target)
是search_leftmost(nums, target + 1) - 1
的一种类型:
def searchRange(self, nums: List[int], target: int) -> List[int]:
left = self.search_leftmost(nums, target)
right = self.search_leftmost(nums, target + 1) - 1
# right = self.search_rightmost(nums, target)
if left != len(nums) and right != len(nums) and nums[left] == nums[right] == target:
return [left, right]
else: return [-1, -1]
一个更简洁的版本是:
def searchRange(self, nums: 'List[int]', target: int) -> 'List[int]':
left = self.search_leftmost(nums, target)
right = self.search_leftmost(nums, target + 1) - 1
return [left, right] if target in nums[left:left + 1] else [-1, -1]
希望对您有所帮助,如果还有其他问题,请发表评论。 :)