leetcode34 Find First and Last Position of Element in Sorted Array
1 """ 2 Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. 3 Your algorithm's runtime complexity must be in the order of O(log n). 4 If the target is not found in the array, return [-1, -1]. 5 Example 1: 6 Input: nums = [5,7,7,8,8,10], target = 8 7 Output: [3,4] 8 Example 2: 9 Input: nums = [5,7,7,8,8,10], target = 6 10 Output: [-1,-1] 11 """ 12 """ 13 我的做法:双指针法 14 """ 15 class Solution: 16 def searchRange(self, nums, target): 17 for i in range(len(nums)): 18 if nums[i] == target: 19 j = i 20 while j < len(nums) and nums[i] == nums[j]:#bug 没有写j<len(nums) 下面j+1会溢出 21 j += 1 22 return [i, j-1] 23 return [-1, -1]