LeetCode Search for Range


class Solution {
private:
int getDirection(int A[], int idx, int target, bool isDefaultBack) {
int r = A[idx] - target;
if (r == 0) {
r = isDefaultBack ? -1 : 1;
}
return r;
}
int getFirstValueIndex(int A[], int n, int target, bool isFromBack) {
int p = -1;
int q = n;
while (p + 1 < q) {
int mid_idx = (p + q) / 2;
int where = getDirection(A, mid_idx, target, isFromBack);
if (where < 0) {
p = mid_idx;
} else {
q = mid_idx;
}
}
if (p != -1 && A[p] != target) {
p = -1;
}
if (q == n || A[q] != target) {
q = -1;
}
return isFromBack ? p : q;
}
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> res;
res.push_back(getFirstValueIndex(A, n, target, false));
res.push_back(getFirstValueIndex(A, n, target, true));
return res;
}
};

进行两次二分查找,一次找upper bound一次找lower bound,如果线性查找的化就不符合要求了。

LeetCode Search for Range,布布扣,bubuko.com

时间: 2024-10-07 15:21:17

LeetCode Search for Range的相关文章

[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 @ 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: 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: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

LeetCode: Search in Rotated Sorted Array

LeetCode: Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

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 of the previous ro

LeetCode——Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2

[LeetCode] Search Insert Position [21]

题目 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6]