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

#include<iostream>
using namespace std;int SearchforRange1(int a[],int n,int k)
{int res = -1;if (n <= 0)return res;int begin = 0; int end = n - 1;while (begin <= end){int mid = (begin + end) / 2;if (a[mid] < k){begin = mid + 1;}else if (a[mid] > k){end = mid - 1;}else{if ((mid-1)<0||a[mid - 1] < k){res = mid;return res;}else{end = mid - 1;}}}return res;
}int SearchforRange2(int a[], int n, int k)
{int res = -1;if (n <= 0)return res;int begin = 0; int end = n - 1;while (begin <= end){int mid = (begin + end) / 2;if (a[mid] < k){begin = mid + 1;}else if (a[mid] > k){end = mid - 1;}else{if ((mid+1)>=n||a[mid + 1] > k){res = mid;return res;}else{begin = mid + 1;}}}return res;
}
int main()
{const int n = 10;int a[n] = { 1, 3, 4, 5, 5, 5, 5, 7, 8, 10 };int k = 1;int begin = SearchforRange1(a, n, k);int end = SearchforRange2(a, n, k);cout << begin << " " << end<< endl;
}

7.1—查找—Sear for a Range相关推荐

  1. 7.2—查找—Sear Insert Position

    描述 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  2. 7.3—查找—Sear a 2D Matrix

    描述 Write an efficient algorithm that searches for a value in an m×n matrix. is matrix has the follo ...

  3. oracle中的数据读取与查找

    数据读取 首先数据块读入到Buffer Cache中,并将其放在LRU(Last Recently Used)链表的MRU(Most Recently Used)端,当需要再次访问该块时可以直接从bu ...

  4. C#:泛型Range助手

    目录 介绍 背景 我们会做什么? 范围(Range)模型 创建整数范围(Range) 使用整数范围(Range) 创建日期时间范围(Range) 使用DateTime范围 下载解决方案 - 7.3 K ...

  5. 找呀找呀找对象(Python查找算法汇总)

    是否还在为如此优秀的你还没有对象而愁掉了头发,是否还在依赖OOP来new出一个对象.其实这个世界缺少的不是对象,而是发现对象的眼睛.当然如此优秀的你也可能只是缺少时间. 本文简要分析4中查找算法,看看 ...

  6. php 二维数组中查找,二维数组中的查找

    一.给出的是方阵 [[1,6,7,8], [3,7,8,9], [9,10,11,12], [12,13,14,15]] 这种情况非常简单,可知对角线元素应为查找元素,如果target大于对角线上某个 ...

  7. lightgbm 决策树 可视化 graphviz

    决策树模型,XGBoost,LightGBM和CatBoost模型可视化 安装 graphviz 参考文档 http://graphviz.readthedocs.io/en/stable/manua ...

  8. Python九十天学习框架,从1到90,从0基础到IQ100

    每个人的基础以及学习进度都不一样,不管最后是90天学会,还是三个月,或是更久,自学按照这个来也能相对系统的学习知识,而不是零散细碎的知识最后无法整合,所以不管怎么样,学习得有个自己的框架,下面一起来看 ...

  9. 决策树和基于决策树的模型构建

    http://graphviz.readthedocs.io/en/stable/manual.html#installation graphviz 安装包下载地址 https://www.graph ...

最新文章

  1. 洛谷P1896 [SCOI2005]互不侵犯 状压dp+位运算
  2. Properties持久的属性集
  3. vs编译cuda DLL
  4. 阿里云服务器被挖矿程序minerd入侵的终极解决办法[转载]
  5. Kubernetes是容器化微服务的圣杯么?
  6. 【CodeForces - 361D】Levko and Array (二分,dp)
  7. uwsgi和nginx的故事
  8. javascript 显示一定范围内的素数(质数)
  9. docker host and docker container
  10. 剑指offer:二叉搜索树的第k个结点(中序遍历)
  11. 【bzoj3530】[Sdoi2014]数数 AC自动机+数位dp
  12. 静态锁 java_为什么锁定对象必须是静态的?
  13. 孙鑫-MFC笔记三--绘图
  14. SSM(Spring+springMVC+MyBatis)框架-springMVC实现图片上传
  15. 爱快软路由拨号移动网络设置
  16. note同步不及时 one_如何提高onenote同步速度?
  17. 怎么绘制等高线计算机软件,绘制等高线(LanDTM)
  18. python runtime错误_使用Django框架遇到RuntimeError: populate() isn't reentrant错误
  19. vb.net 简单取摄像头图片_【图片】大车监控如何安装?_大车监控吧
  20. 做好这三步让你云主机更安全!

热门文章

  1. 阿里内核月报2014年2月
  2. python按照日期筛选excel_【Python代替Excel】6:按条件筛选
  3. 2019-2020南邮《信息安全数学基础》期末考试回忆
  4. 阿里云混合云重磅发布 全栈建云智能管云极致用云
  5. 女程序员南漂3年:有种心酸叫孤单的人总是晚回家
  6. 传说中的Linux三剑客命令之grep(三剑客老幺)别人只知道了而我却做到了
  7. 赛迪网:VMware访谈实录
  8. layui upload上传携带额外参数
  9. unity的C#学习——标识符号、关键字与数据类型及其转换方式
  10. 文字转语音软件哪个好?看完这篇你就知道了