LintCode 61-搜索区间
来源:互联网 发布:淘宝店铺换ip有影响吗 编辑:程序博客网 时间:2024/06/01 12:49
本人电子系,只为一学生。心喜计算机,小编以怡情。
给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置。
如果目标值不在数组中,则返回[-1, -1]
样例
给出[5, 7, 7, 8, 8, 10]和目标值target=8,
返回[3, 4]
public int[] searchRange(int[] A, int target) { // write your code here if(A.length==1) return new int [2] ;//迷之一个元素测试 int ret[]={-1,-1};//找不到的返回[-1,-1] int low=0; int high=A.length-1; int mid = 0;//接下来要用带mid,因此设置为全局变量 while(low<=high) { //很正常的二分法套路 mid=(low+high)/2; if(A[mid]<target) { low=mid+1; } if(A[mid]>target) high=mid-1; if(A[mid]==target) break; } if(low>high) return ret;//找不到就返回[-1,-1] //二分法可能找到的是中间的位置,因此向两边搜索找起始结束位置 int i=mid-1; while(i>-1&&A[i]==A[mid]) i--; ret[0]=i+1;//起始 i=mid+1; while(i<A.length&&A[i]==A[mid]) i++; ret[1]=i-1;//结束 return ret;//找到的返回结果 }
0 0
- lintcode-搜索区间-61
- LintCode 61-搜索区间
- lintcode(61)搜索区间
- LintCode 61 搜索区间
- LintCode-剑指Offer-(61)搜索区间
- LintCode-搜索区间
- Lintcode搜索区间
- LintCode : 搜索区间
- LintCode:搜索区间
- LintCode : 搜索区间
- Lintcode 搜索区间
- [LintCode]61.搜索区间
- LintCode 搜索区间
- 搜索区间——LintCode
- LintCode——搜索区间
- [Lintcode]Search for a Range搜索区间
- Lintcode 二叉查找树中搜索区间
- lintcode-二叉查找树中搜索区间-11
- 第7章 复用类
- danielgindi/Charts饼状图pieChartView用法
- ffmpeg转换参数和压缩输出大小的比率 参考 最新版本FFMPEG
- mysql-5.7.17-winx64.zip 的安装与配置
- Zsh报错: zsh: command not found:tree
- LintCode 61-搜索区间
- VMware中ubuntu忘记密码的解决办法
- HashMap学习笔记
- Can a Compound Index serve multiple queries
- 【备忘】北风网Android应用实战淘宝网手机客户端全程实录 下载
- AndroidAnnotations注解说明
- pat-a1084. Broken Keyboard (20)
- 常用的表扩展字段方案
- Python 装饰器归纳总结