Article Directory:
- topic
- A script
- A script logic
topic:
Nums Given an array of integers arranged in ascending order, according to the target and one target value. Identify a given target start and end positions in the array.
Your time complexity of the algorithm must be O (log n) level.
If the target is not present in the array, return [-1, -1].
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1, -1]
A script: [when using: 96ms]
class Solution: def searchRange(self, nums: List[int], target: int) -> List[int]: list1 = [] n1 = len(nums) if target in nums: list1.append(nums.index(target)) nums.reverse() list1.append(n1 - nums.index(target) -1) return(list1) else: return([-1,-1])
A script logic:
- First: the if statement determines whether the list contains the destination number;
- Second: if so, index list of the method of obtaining the index value through a first target element in the target list, and add them to the list of the registration result; and then by reversing the target list, after obtaining the first inverted again a target element index, and by calculation, to add it to the list of the recording target element
- Third: If not, the process directly returns [-1, -1]