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. 【线段树】Codeforces Round #393 (Div. 1) C. Nikita and stack

    就是给你一些元素的进栈 出栈操作,不按给定的顺序,要求你对于每次输入,都依据其及其之前的输入,判断出栈顶的元素是谁. 用线段树维护,每次push,将其位置的值+1,pop,将其位置的值-1.相当于寻找 ...

  2. 【递推】【卡特兰数】CODEVS 3134 Circle

    新GET了一种卡特兰数的应用…… 在一个圆上,有2*K个不同的结点,我们以这些点为端点,连K条线段,使得每个结点都恰好用一次.在满足这些线段将圆分成最少部分的前提下,请计算有多少种连线的方法. 不会证 ...

  3. 【最短路】【spfa】CODEVS 2645 Spore

    spfa最短路+判负权回路(是否某个点入队超过n次). #include<cstdio> #include<queue> #include<cstring> usi ...

  4. python3 开发面试题(常用模块以及第三方库)6.5

    """ 1. os和sys都是干什么的? 2. 你工作中都用过哪些内置模块? 3. 有没有用过functools模块? """ #sys模块 ...

  5. 微信小程序退款【证书的使用】

    1,官方文档的地址 2,在官方文档中给出了证书使用的链接,如下: [其实只有证书的获取,选择.具体的证书怎么在代码中使用,文档中并没有给出说明] 3,第一步准备请求的参数,里面只有五个是参数是有点特殊 ...

  6. 【java】递归统计本地磁盘所有文件,提取重复文件,JDK8 map迭代

    package com.sxd.createDao; import java.io.File; import java.time.LocalDateTime; import java.util.Has ...

  7. Android 中 Environment.getExternalStorageDirectory()无效

    我们在处理缓存的时候,并不是每次都会在应用私有存储空间那里保存,很多时候是需要用到ExternalStorage.我们平时一般都是用Environment.getExternalStorageDire ...

  8. 自己做的roguelike+恶魔城游戏《魔塔猎人》已发布。

    游戏仍然是标准的roguelike,死亡后回到出生点重新开始,宏观架构上参考了<死亡细胞>,战斗设计上更加强调轻重攻击的组合,再配合236和28系列的搓招技.空中的突进飞腿.副武器等等. ...

  9. Linq-语句之Select/Distinct和Count/Sum/Min/Max/Avg

    上一篇讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ to SQL语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to Data ...

  10. php中将SimpleXMLElement Object数组转化为普通数组

    做微信开发,鉴于微信POST的消息是XML数据包,通过SimpleXMLElement Object获取的数据不好操作,需要转化为普通数组. 网上找了很多方法都不理想,发现通过json_decode和 ...