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.

解法一:

  1. class Solution {
  2. public:
  3. /*
  4. * @param A: an integer sorted array
  5. * @param target: an integer to be inserted
  6. * @return: a list of length 2, [index1, index2]
  7. */
  8. vector<int> searchRange(vector<int> &A, int target) {
  9. if (A.empty()) {
  10. return vector<int>(, -);
  11. }
  12.  
  13. vector<int> result;
  14.  
  15. //find first
  16. int start = ;
  17. int end = A.size() - ;
  18.  
  19. while (start + < end) {
  20. int mid = start + (end - start) / ;
  21. if (A[mid] == target) {
  22. end = mid;
  23. }
  24. else if (A[mid] < target) {
  25. start = mid;
  26. }
  27. else if (A[mid] > target) {
  28. end = mid;
  29. }
  30. }
  31.  
  32. if (A[start] == target) {
  33. result.push_back(start);
  34. }
  35. else if (A[end] == target) {
  36. result.push_back(end);
  37. }
  38. else {
  39. return vector<int>(, -);
  40. }
  41.  
  42. //find last
  43. start = ;
  44. end = A.size() - ;
  45.  
  46. while (start + < end) {
  47. int mid = start + (end - start) / ;
  48. if (A[mid] == target) {
  49. start = mid;
  50. }
  51. else if (A[mid] < target) {
  52. start = mid;
  53. }
  54. else if (A[mid] > target) {
  55. end = mid;
  56. }
  57. }
  58.  
  59. if (A[end] == target) {
  60. result.push_back(end);
  61. }
  62. else if (A[start] == target) {
  63. result.push_back(start);
  64. }
  65.  
  66. return result;
  67. }
  68. };

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. codevs 4163 求逆序对的数目 -树状数组法

    4163 hzwer与逆序对  时间限制: 10 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题目描述 Description hzwer在研究逆序对. 对于数列{a},如果 ...

  2. 数据挖掘经典算法——K-means算法

    算法描述 K-means算法是一种被广泛使用的基于划分的聚类算法,目的是将n个对象会分成k个簇.算法的具体描述如下: 随机选取k个对象作为簇中心: Do 计算所有对象到这k个簇中心的距离,将距离最近的 ...

  3. iptables禁止外网访问redis server服务默认端口6379的命令

    //只允许127.0.0.1访问6379 iptables -A INPUT -s 127.0.0.1 -p tcp --dport 6379 -j ACCEPT //其他ip访问全部拒绝 iptab ...

  4. spring+activity+mysql集群

     第一步:先配置好第一个activityMQ 在broker外面加入数据库的连接信息,并将mysql的mysql-connector-java.jar,即java连接mysql的jar包放入apach ...

  5. @import url(../image/css)的用法

    1.@import url(../image/css);可以加载css文件2.@import url(../image/css);可以写在html里加载css文件,也可以写在css文件里加载css文件 ...

  6. Lucene的学习及使用实验

    实验一下Lucene是怎么使用的. 参考:http://www.importnew.com/12715.html (例子比较简单) http://www.yiibai.com/lucene/lucen ...

  7. Ubuntu16.04安装Pytorch

    一.安装 1. 官方github:https://github.com/pytorch/pytorch Install optional dependencies //安装依赖项 On Linux e ...

  8. EasyUI-解决EasyUI 加载两次url的问题

    1.传统方式 $(function () { var url = "../Source/Query/jhDataQry.ashx?action=query"; $(dg).data ...

  9. easyUI表头样式

    easyUI表头样式 学习了:https://blog.csdn.net/lucasli2016/article/details/53606609 easyUI的样式定义在easyui.css中 表头 ...

  10. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-Switch Case语句是否会自动跳转到下一个

    在C#中,每一个case后面必须有break,所以输出1,也就是如果a=0,则只会执行case=0的那一段,当等于1之后不会继续.   在TwinCAT中,虽然CASE语句没有break,但是实际上不 ...