61. Search for a Range【medium】
61. Search for a Range【medium】
Given a sorted array of n integers, find the starting and ending position of a given target value.
If the target is not found in the array, return [-1, -1].
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
O(log n) time.
解法一:
class Solution {
public:
/*
* @param A: an integer sorted array
* @param target: an integer to be inserted
* @return: a list of length 2, [index1, index2]
*/
vector<int> searchRange(vector<int> &A, int target) {
if (A.empty()) {
return vector<int>(, -);
}
vector<int> result;
//find first
int start = ;
int end = A.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (A[mid] == target) {
end = mid;
}
else if (A[mid] < target) {
start = mid;
}
else if (A[mid] > target) {
end = mid;
}
}
if (A[start] == target) {
result.push_back(start);
}
else if (A[end] == target) {
result.push_back(end);
}
else {
return vector<int>(, -);
}
//find last
start = ;
end = A.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (A[mid] == target) {
start = mid;
}
else if (A[mid] < target) {
start = mid;
}
else if (A[mid] > target) {
end = mid;
}
}
if (A[end] == target) {
result.push_back(end);
}
else if (A[start] == target) {
result.push_back(start);
}
return result;
}
};
61. Search for a Range【medium】的更多相关文章
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 74. First Bad Version 【medium】
74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- 28. Search a 2D Matrix 【easy】
28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...
- 【Leetcode】【Medium】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- 【枚举】bzoj1643 [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪
显然<=n的平方数只有sqrt(n)个,三重循环枚举三块草坪,再减一下验证最后一块是不是平方数.O(n*sqrt(n)). #include<cstdio> #include< ...
- 【树链剖分】【dfs序】【线段树】bzoj2836 魔法树
这道题告诉我们:树链剖分的重标号就是dfs序. #include<cstdio> #include<algorithm> using namespace std; #defin ...
- 【floyd】CODEVS 1077 多源最短路
floyd模板 #include<cstdio> #include<algorithm> using namespace std; ][],m,x,y,n; int main( ...
- 8.3(java学习笔记)动态编译(DynamicCompiler)与动态运行(DynamicRun)
一.动态编译 简单的说就是在运行一个java程序的过程中,可以通过一些API来编译其他的Java文件. 下面主要说动态编译的实现: 1.获取java编译编译器 2.运行编译器(须指定编译文件) 获取编 ...
- MyBasic架构
MyBasic架构图 2,架构图解析 (1) (2) (3)
- 【转载】Mini6410启动过程
这段时间在尝试使用uBoot来替代友善的Superboot,让板子支持从SD卡启动,所以就仔细研究了一下友善提供的内核和它的启动参数,发现 友善真的蛮聪明,把电脑的启动方式借鉴到它们自己的开发板上了. ...
- Go beego框架使用笔记(一)
Beego介绍 beego我认为是go初学者比较容易上手的一门MVC Web框架.简单易懂,最重要的一点就是提供了中文文档,这对于我这种英语能力比较差的人来说就是福音. beego的官网上是这么介绍b ...
- SQLite 使用技巧
http://blog.csdn.net/beifengdelei/article/details/7166056 SQLite自增ID自段使用方法为 INTEGER PRIMARY KEY AUTO ...
- 快速开发框架(FDMS)新增1000个对外接口都不须要手写一行代码
一个大型系统难免会跟其它系统有数据交换,这里就要提供数据接口给外部系统. 曾经在一家智能终端设备的公司上班.那段时间的主要工作就是写接口.接口须要与手机.手持设备.系统管理软件等进行数据交换.总结了一 ...
- git如何打补丁?
git cherry-pick 可以把某个分支的某几次提交合入到当前分支,只是在一台设备上操作. git format-patch 可以把某个分支的n次提交分别打成n个补丁,然后把这些补丁文件(比如0 ...