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

Have you met this question in a real interview?

Yes
Example

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

Challenge

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】的更多相关文章

  1. 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. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

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

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

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

  6. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

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

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

  9. 【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 ...

随机推荐

  1. [COGS2479 && COGS2639]高维偏序(CDQ分治,bitset)

    COGS2479:四维偏序. CDQ套CDQ CDQ:对a分治,对b排序,再对a打标记,然后执行CDQ2 CDQ2:对b分治,对c归并排序,对d树状数组. #include<cstdio> ...

  2. [BZOJ3206][APIO2013]道路费用(最小生成树)

    3206: [Apio2013]道路费用 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 568  Solved: 266[Submit][Status ...

  3. HDOJ 4812 D Tree

    Discription There is a skyscraping tree standing on the playground of Nanjing University of Science ...

  4. 【扩展欧几里得】poj2115 C Looooops

    题意大概是让你求(A+Cx) mod 2^k = B的最小非负整数解. 若(B-A) mod gcd(C,2^k) = 0,就有解,否则无解. 式子可以化成Cx + 2^k*y = B - A,可以用 ...

  5. 【手动开栈】【dfs序】【树状数组】【Tarjan】bzoj2819 Nim

    考虑树状数组区间修改(只对其子树的答案有影响)点查询,每个点记录的是它到根路径上的权值异或和. 答案时query(L)^query(R)^a[lca]. 这种方法在支持区间加法.减法的树上询问的时候可 ...

  6. 【kmp算法】【Rabin-Karp算法】bzoj2462 [BeiJing2011]矩阵模板

    算法就不说了,反正是基于字符串匹配的.这里比较一下kmp和Rabin-Karp算法. <法一>kmp算法. 592788 lizitong 2462 Accepted 4828 kb 68 ...

  7. 【KMP】BZOJ3670-[Noi2014]动物园

    [题目大意][依然借用别人的概括]给定一个长为L的字符串(L<=100W),求一个num数组,num[i]表示长度为i的前缀中字符串S’的数量,其中S‘既是该前缀的前缀也是该前缀的后缀,且|S' ...

  8. Java高级架构师(一)第24节:加入ehcache,把工程加入到Git

    ehcache的maven配置: <!-- ehcache的jar --> <dependency> <groupId>net.sf.ehcache</gro ...

  9. golang技巧-接口型函数

    接口型函数:指的是用函数实现接口,这样在调用的时候就会非常简便,这种函数为接口型函数,这种方式适用于只有一个函数的接口. 定义一个类型,这个类型只定义了函数的参数列表,函数参数列表与接口定义的方法一致 ...

  10. 使用原生JS进行字符串转对象

    字符串转对象 目的 工作中如果需要原生 JS 完成字符转对象的话可以通过 JSON.parse(str), 但是这个方法是ES5中才出现, 如果需要兼容低版本就需要其它方法 使用原生 JS 解决字符串 ...