Difficult:Medium
題目
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1].
You must write an algorithm with O(log n) runtime complexity.
翻譯
給定一個按非降序排序的整數數組 nums,找到給定目標值的開始和結束位置。
如果在數組中找不到目標,則返回 [-1, -1]。
您必須編寫一個具有 O(log n) 運行時復雜度的算法。
範例
Example 1:
1 | Input: nums = [5,7,7,8,8,10], target = 8 |
Example 2:
1 | Input: nums = [5,7,7,8,8,10], target = 6 |
Example 3:
1 | Input: nums = [], target = 0 |
解題思路
Solution
Code 1 :
1 | var searchRange = function (nums, target) { |