leetcode 二分查找 Search for a Range

Search for a Range

Total Accepted: 21480 Total
Submissions: 78454My Submissions

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].

题意:在一个排好序的数组,找给定值的起始下标和结束下标

思路:二分查找

先用lower_bound找到起始下标

再用upper_bound找到结束下标

lower_bound和upper_bound的实现参见《stl源码剖析》

复杂度:时间O(log n) 空间O(1)

vector<int> searchRange(int A[], int n, int target){
	int l = distance(A, lower_bound(A, A + n, target));
	int u = distance(A, upper_bound(A, A + n, target));
	vector<int> res;
	if(A[l] != target){
		res.push_back(-1);
		res.push_back(-1);
	}else{
		res.push_back(l);
		res.push_back(u - 1);
	}
	return res;
}
时间: 2024-08-06 03:14:04

leetcode 二分查找 Search for a Range的相关文章

Leetcode 二分查找 Search a 2D Matrix

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search a 2D Matrix Total Accepted: 10871 Total Submissions: 35629 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in e

leetcode 二分查找 Search in Rotated Sorted Array

Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions 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 s

leetcode 二分查找 Search in Rotated Sorted ArrayII

Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a functi

Leetcode 二分查找 Search Insert Position

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 Total Submissions: 41575 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 i

[leetcode]二分查找总结

Search for a Range 1.最简单的想法,用最普通的二分查找,找到target,然后向左右扩张,大量的重复的target,就会出现O(n)效率. class Solution { public int[] searchRange(int[] A, int target) { int ans[]=new int[2]; int a= bserch(A,target); if(a==-1) {ans[0]=-1;ans[1]=-1; return ans;} //left int b=

LeetCode OJ: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][Python]34: Search for a Range

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 34: Search for a Rangehttps://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 runt

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