LeetCode: 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 example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

SOLUTION 1:

使用改进的二分查找法。终止条件是:left < right - 1 这样结束的时候,会有2个值供我们判断。这样做的最大的好处是,不用处理各种越界问题。

感谢黄老师写出这么优秀的算法:http://answer.ninechapter.com/solutions/search-for-a-range/

请同学们一定要记住这个二分法模板,相当好用哦。

1. 先找左边界。当mid == target,将right移动到mid,继续查找左边界。

最后如果没有找到target,退出

2. 再找右边界。当mid == target,将left移动到mid,继续查找右边界。

最后如果没有找到target,退出

 1 public class Solution {
 2     public int[] searchRange(int[] A, int target) {
 3         int[] ret = {-1, -1};
 4
 5         if (A == null || A.length == 0) {
 6             return ret;
 7         }
 8
 9         int len = A.length;
10         int left = 0;
11         int right = len - 1;
12
13         // so when loop end, there will be 2 elements in the array.
14         // search the left bound.
15         while (left < right - 1) {
16             int mid = left + (right - left) / 2;
17             if (target == A[mid]) {
18                 // 如果相等,继续往左寻找边界
19                 right = mid;
20             } else if (target > A[mid]) {
21                 // move right;
22                 left = mid;
23             } else {
24                 right = mid;
25             }
26         }
27
28         if (A[left] == target) {
29             ret[0] = left;
30         } else if (A[right] == target) {
31             ret[0] = right;
32         } else {
33             return ret;
34         }
35
36         left = 0;
37         right = len - 1;
38         // so when loop end, there will be 2 elements in the array.
39         // search the right bound.
40         while (left < right - 1) {
41             int mid = left + (right - left) / 2;
42             if (target == A[mid]) {
43                 // 如果相等,继续往右寻找右边界
44                 left = mid;
45             } else if (target > A[mid]) {
46                 // move right;
47                 left = mid;
48             } else {
49                 right = mid;
50             }
51         }
52
53         if (A[right] == target) {
54             ret[1] = right;
55         } else if (A[left] == target) {
56             ret[1] = left;
57         } else {
58             return ret;
59         }
60
61         return ret;
62     }
63 }

时间: 2024-08-04 04:01:53

LeetCode: Search for a Range 解题报告的相关文章

LeetCode: Search a 2D Matrix 解题报告

Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer

[leetcode]Search for a Range @ Python

原题地址:https://oj.leetcode.com/problems/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

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

[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, 7,

LeetCode: Search for a Range [033]

[题目] 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 排序数组中寻找目标下标范围(AC)

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, 7,

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of

【LeetCode】Unique Paths II 解题报告

[题目] Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the