标签:index number previous 过程 nod 位置 修改 cti ==
给定一个按照升序排列的整数数组 nums
,和一个目标值 target
。找出给定目标值在数组中的开始位置和结束位置。
你的算法时间复杂度必须是 O(log n) 级别。
如果数组中不存在目标值,返回 [-1, -1]
。
示例 1:
输入: nums = [5,7,7,8,8,10]
, target = 8
输出: [3,4]
示例 2:
输入: nums = [5,7,7,8,8,10]
, target = 6
输出: [-1,-1]
想法
对 target
检查每一个下标,一定能得到正确答案。
算法
首先,我们对 nums
数组从左到右做线性遍历,当遇到 target
时中止。如果我们没有中止过,那么 target
不存在,我们可以返回“错误代码” [-1, -1]
。如果我们找到了有效的左端点坐标,我们可以坐第二遍线性扫描,但这次从右往左进行。这一次,第一个遇到的 target
将是最右边的一个(因为最左边的一个存在,所以一定会有一个最右边的 target
)。我们接下来只需要返回这两个坐标。
class Solution {
public int[] searchRange(int[] nums, int target) {
int[] targetRange = {-1, -1};
<span class="hljs-comment">// find the index of the leftmost appearance of `target`.</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < nums.length; i++) {
<span class="hljs-keyword">if</span> (nums[i] == target) {
targetRange[<span class="hljs-number">0</span>] = i;
<span class="hljs-keyword">break</span>;
}
}
<span class="hljs-comment">// if the last loop did not find any index, then there is no valid range</span>
<span class="hljs-comment">// and we return [-1, -1].</span>
<span class="hljs-keyword">if</span> (targetRange[<span class="hljs-number">0</span>] == -<span class="hljs-number">1</span>) {
<span class="hljs-keyword">return</span> targetRange;
}
<span class="hljs-comment">// find the index of the rightmost appearance of `target` (by reverse</span>
<span class="hljs-comment">// iteration). it is guaranteed to appear.</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = nums.length-<span class="hljs-number">1</span>; j >= <span class="hljs-number">0</span>; j--) {
<span class="hljs-keyword">if</span> (nums[j] == target) {
targetRange[<span class="hljs-number">1</span>] = j;
<span class="hljs-keyword">break</span>;
}
}
<span class="hljs-keyword">return</span> targetRange;
}
}
class Solution:
def searchRange(self, nums, target):
# find the index of the leftmost appearance of target
. if it does not
# appear, return [-1, -1] early.
for i in range(len(nums)):
if nums[i] == target:
left_idx = i
break
else:
return [-1, -1]
<span class="hljs-comment"># find the index of the rightmost appearance of `target` (by reverse</span>
<span class="hljs-comment"># iteration). it is guaranteed to appear.</span>
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(len(nums)<span class="hljs-number">-1</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">-1</span>):
<span class="hljs-keyword">if</span> nums[j] == target:
right_idx = j
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">return</span> [left_idx, right_idx]
复杂度分析
时间复杂度: 。
这个暴力解法检测了num
数组中每个元素恰好两次,所以总运行时间是线性的。
空间复杂度: 。
线性扫描方法使用了固定大小的数组和几个整数,所以它的空间大小为常数级别的。
想法
因为数组已经排过序了,我们可以使用二分查找的方法去定位左右下标。
算法
总体算法工作过程与线性扫描方法类似,除了找最左和最右下标的方法。这里我们仅仅做几个微小的调整,用这种修改过的二分查找方法去搜索这个排过序的数组。首先,为了找到最左边(或者最右边)包含 target
的下标(而不是找到的话就返回 true
),所以算法在我们找到一个 target
后不能马上停止。我们需要继续搜索,直到 lo == hi
且它们在某个 target
值处下标相同。
另一个改变是 left
参数的引入,它是一个 boolean 类型的变量,指示我们在遇到 target == nums[mid]
时应该做什么。如果 left
为 true
,那么我们递归查询左区间,否则递归右区间。考虑如果我们在下标为 i
处遇到了 target
,最左边的 target
一定不会出现在下标大于 i
的位置,所以我们永远不需要考虑右子区间。当求最右下标时,道理同样适用。
class Solution {
// returns leftmost (or rightmost) index at which `target` should be
// inserted in sorted array `nums` via binary search.
private int extremeInsertionIndex(int[] nums, int target, boolean left) {
int lo = 0;
int hi = nums.length;
<span class="hljs-keyword">while</span> (lo < hi) {
<span class="hljs-keyword">int</span> mid = (lo + hi) / <span class="hljs-number">2</span>;
<span class="hljs-keyword">if</span> (nums[mid] > target || (left && target == nums[mid])) {
hi = mid;
}
<span class="hljs-keyword">else</span> {
lo = mid+<span class="hljs-number">1</span>;
}
}
<span class="hljs-keyword">return</span> lo;
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span>[] searchRange(<span class="hljs-keyword">int</span>[] nums, <span class="hljs-keyword">int</span> target) {
<span class="hljs-keyword">int</span>[] targetRange = {-<span class="hljs-number">1</span>, -<span class="hljs-number">1</span>};
<span class="hljs-keyword">int</span> leftIdx = extremeInsertionIndex(nums, target, <span class="hljs-keyword">true</span>);
<span class="hljs-comment">// assert that `leftIdx` is within the array bounds and that `target`</span>
<span class="hljs-comment">// is actually in `nums`.</span>
<span class="hljs-keyword">if</span> (leftIdx == nums.length || nums[leftIdx] != target) {
<span class="hljs-keyword">return</span> targetRange;
}
targetRange[<span class="hljs-number">0</span>] = leftIdx;
targetRange[<span class="hljs-number">1</span>] = extremeInsertionIndex(nums, target, <span class="hljs-keyword">false</span>)-<span class="hljs-number">1</span>;
<span class="hljs-keyword">return</span> targetRange;
}
}
class Solution:
# returns leftmost (or rightmost) index at which target
should be inserted in sorted
# array nums
via binary search.
def extreme_insertion_index(self, nums, target, left):
lo = 0
hi = len(nums)
<span class="hljs-keyword">while</span> lo < hi:
mid = (lo + hi) // <span class="hljs-number">2</span>
<span class="hljs-keyword">if</span> nums[mid] > target <span class="hljs-keyword">or</span> (left <span class="hljs-keyword">and</span> target == nums[mid]):
hi = mid
<span class="hljs-keyword">else</span>:
lo = mid+<span class="hljs-number">1</span>
<span class="hljs-keyword">return</span> lo
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">searchRange</span><span class="hljs-params">(self, nums, target)</span>:</span>
left_idx = self.extreme_insertion_index(nums, target, <span class="hljs-literal">True</span>)
<span class="hljs-comment"># assert that `left_idx` is within the array bounds and that `target`</span>
<span class="hljs-comment"># is actually in `nums`.</span>
<span class="hljs-keyword">if</span> left_idx == len(nums) <span class="hljs-keyword">or</span> nums[left_idx] != target:
<span class="hljs-keyword">return</span> [<span class="hljs-number">-1</span>, <span class="hljs-number">-1</span>]
<span class="hljs-keyword">return</span> [left_idx, self.extreme_insertion_index(nums, target, <span class="hljs-literal">False</span>)<span class="hljs-number">-1</span>]
复杂度分析
时间复杂度: 。
由于二分查找每次将搜索区间大约划分为两等分,所以至多有 次迭代。二分查找的过程被调用了两次,所以总的时间复杂度是对数级别的。
空间复杂度: 。
所有工作都是原地进行的,所以总的内存空间是常数级别的。
更多:
https://www.jianshu.com/p/bcd11d5ba74d
leetcode题解之34. 在排序数组中查找元素的第一个和最后一个位置
标签:index number previous 过程 nod 位置 修改 cti ==
原文地址:https://www.cnblogs.com/leetcodetijie/p/13195132.html