7.1—查找—Sear for a Range
描述
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相关推荐
- 7.2—查找—Sear Insert Position
描述 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 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 ...
- oracle中的数据读取与查找
数据读取 首先数据块读入到Buffer Cache中,并将其放在LRU(Last Recently Used)链表的MRU(Most Recently Used)端,当需要再次访问该块时可以直接从bu ...
- C#:泛型Range助手
目录 介绍 背景 我们会做什么? 范围(Range)模型 创建整数范围(Range) 使用整数范围(Range) 创建日期时间范围(Range) 使用DateTime范围 下载解决方案 - 7.3 K ...
- 找呀找呀找对象(Python查找算法汇总)
是否还在为如此优秀的你还没有对象而愁掉了头发,是否还在依赖OOP来new出一个对象.其实这个世界缺少的不是对象,而是发现对象的眼睛.当然如此优秀的你也可能只是缺少时间. 本文简要分析4中查找算法,看看 ...
- php 二维数组中查找,二维数组中的查找
一.给出的是方阵 [[1,6,7,8], [3,7,8,9], [9,10,11,12], [12,13,14,15]] 这种情况非常简单,可知对角线元素应为查找元素,如果target大于对角线上某个 ...
- lightgbm 决策树 可视化 graphviz
决策树模型,XGBoost,LightGBM和CatBoost模型可视化 安装 graphviz 参考文档 http://graphviz.readthedocs.io/en/stable/manua ...
- Python九十天学习框架,从1到90,从0基础到IQ100
每个人的基础以及学习进度都不一样,不管最后是90天学会,还是三个月,或是更久,自学按照这个来也能相对系统的学习知识,而不是零散细碎的知识最后无法整合,所以不管怎么样,学习得有个自己的框架,下面一起来看 ...
- 决策树和基于决策树的模型构建
http://graphviz.readthedocs.io/en/stable/manual.html#installation graphviz 安装包下载地址 https://www.graph ...
最新文章
- 洛谷P1896 [SCOI2005]互不侵犯 状压dp+位运算
- Properties持久的属性集
- vs编译cuda DLL
- 阿里云服务器被挖矿程序minerd入侵的终极解决办法[转载]
- Kubernetes是容器化微服务的圣杯么?
- 【CodeForces - 361D】Levko and Array (二分,dp)
- uwsgi和nginx的故事
- javascript 显示一定范围内的素数(质数)
- docker host and docker container
- 剑指offer:二叉搜索树的第k个结点(中序遍历)
- 【bzoj3530】[Sdoi2014]数数 AC自动机+数位dp
- 静态锁 java_为什么锁定对象必须是静态的?
- 孙鑫-MFC笔记三--绘图
- SSM(Spring+springMVC+MyBatis)框架-springMVC实现图片上传
- 爱快软路由拨号移动网络设置
- note同步不及时 one_如何提高onenote同步速度?
- 怎么绘制等高线计算机软件,绘制等高线(LanDTM)
- python runtime错误_使用Django框架遇到RuntimeError: populate() isn't reentrant错误
- vb.net 简单取摄像头图片_【图片】大车监控如何安装?_大车监控吧
- 做好这三步让你云主机更安全!