LeetCode 34. Search for a Range (找到一个范围)

Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm‘s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].



题目标签:Array

  这道题目给了我们一个 有序序列,其中是有重复的,让我们找到target的范围,如果没有target,就返回 [-1,-1]。既然规定我们用O(log n),那么肯定是利用binary search。问题是,用binary search 是可以找到target, 但是这里需要找到一个范围,中间可能有很多个重复的target。我们可以用两次binary search, 第一次就找到第一个target, 第二次找到最后一个target,这样就可以返回target的范围了。

  怎么来找到第一个target呢,分析一下,如果中间的数字 大于 target, 那么说明target 还在更左边, 我们要把 right = mid - 1。如果中间数字 小于 target, 说明target 在更右边,要把 left = mid + 1。 到这里为止,都是普通的二分法,那么如果中间的数字是等于 target 的呢,因为我们要找到第一个target的位置,所以,就算找到了target, 我们也要向左边移动,因此,要把这个等于的情况加入 大于里面去。因为它们两种情况都是向左边移动。每次找到target 的时候,还需要 记住它的位置,之后一直更新。因为你找到的target 不一定是最左边的,所以要一直更新,最后一次就是最左边的target位置。

  找最后一个target也是同理。

Java Solution:

Runtime beats 79.19%

完成日期:07/14/2017

关键词:Array

关键点:用两次二分法,第一次来找最左边的target,第二次来找最右边的target

 1 public class Solution
 2 {
 3     public int[] searchRange(int[] nums, int target)
 4     {
 5         int[] res = {-1, -1};
 6
 7         // first binary search to find the first target
 8         int left = 0;
 9         int right = nums.length - 1;
10
11         while(left <= right)
12         {
13             int mid = left + (right - left) / 2;
14             if(nums[mid] >= target) // if mid one is greater or equal to target
15                 right = mid - 1;    // move to left half
16             else                    // if mid one is less than target
17                 left = mid + 1;        // move to right half
18
19             if(nums[mid] == target) // update index
20                 res[0] = mid;
21         }
22
23         // second binary search to find the last target
24         right = nums.length - 1;
25
26         while(left <= right)
27         {
28             int mid = left + (right - left) / 2;
29             if(nums[mid] <= target)
30                 left = mid + 1;
31             else
32                 right = mid - 1;
33
34             if(nums[mid] == target)
35                 res[1] = mid;
36         }
37
38         return res;
39     }
40 }

参考资料:

https://leetcode.com/problems/search-for-a-range/#/discuss

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

时间: 2024-10-23 18:38:17

LeetCode 34. Search for a Range (找到一个范围)的相关文章

[LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(l

leetCode 34.Search for a Range (搜索范围) 解题思路和方法

Search for a Range Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For ex

Leetcode 34 Search for a Range (二分搜索 lower_bound和upper_bound)

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order ofO(logn). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7, 7

LeetCode 34 Search for a Range (C,C++,Java,Python)

Problem: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Giv

LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定数字的起止下标 采用两遍扫描: 第一遍扫描得到给定数字的起始下标,(从下标i==0开始到nums.lenght-1) 第二遍扫描从第一遍扫描得到的下标开始进行扫描 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 数组中找到targe

Java [leetcode 34]Search for a Range

题目描述: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given

leetcode题解:Search for a Range (已排序数组范围查找)

题目: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example,Given [5,

[LeetCode] 034. Search for a Range (Medium) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search for a Range (Medium) 链接: 题目:https://leetcode.com/problems/search-for-a-range/ 代码(github):https://github.com/illuz/leetcode 题意: 在有序数组中找到一个数的范围.(由于数