leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search 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].
思路:此题在思考的时候走了些弯路,一心想着一个循环解决这个问题。可是写代码的时候总是不能非常好的解出。最后突然想起来。全然能够先二分查找最低的位置。然后再查找最高位置就可以,这样就非常easy了。只是里面还是有一些细节须要注意。
详细代码例如以下:
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] ans = new int[]{-1,-1};
//排除特殊情况
if(nums.length == 0 || nums[0] > target || nums[nums.length-1] < target)
return ans;
//首尾都相等
if(nums[0]== target && nums[nums.length-1] == target){
ans[0] = 0;
ans[1] = nums.length - 1;
return ans;
}
//二分查找
int low = 0;
int hight = nums.length - 1;
int mid = 0;
//先求符合要求的起始值
while(low <= hight){
mid = (low + hight)/2;
if(nums[mid] > target){
hight = mid -1;
}else if(nums[mid] < target){
low = mid + 1;
}else{
hight = mid;
}//推断结束情况
if(mid > 0 && nums[mid] == target && nums[mid -1] < target){
break;
}else if(mid == 0 && nums[mid] == target){
break;
}
}
//是否须要赋值。假设最低位置不存在,那么最高位置也不存在
if(nums[mid] == target){
ans[0] = mid;
//再求符合要求的最大位置
low = mid;//起始值设为target的最低位置
hight = nums.length - 1;
while(low <= hight){
mid = (low + hight)/2;
if(mid < nums.length - 1 && nums[mid + 1] == target){
mid ++;//这里非常关键,由于(low+hight)/2自己主动向下取整的,所以看情况+1或向上取整
}
//分情况更新位置
if(nums[mid] > target){
hight = mid -1;
}else if(nums[mid] < target){
low = mid + 1;
}else{
low = mid;
}
//推断最高位置
if(mid <nums.length-1 && nums[mid] == target && nums[mid +1] > target){
break;
}else if(mid == nums.length-1 && nums[mid] == target){
break;
}
}
ans[1] = mid;//最低位存在。最高位肯定也存在
}
return ans;
}
}
leetCode 34.Search for a Range (搜索范围) 解题思路和方法的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- [leetcode 34] search for a range
1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- Java [leetcode 34]Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
随机推荐
- 关于C++的子类指针指向父类
基类指针引用派生类对象 用基类指针引用一个派生类对象,由于派生类对象也是基类的对象,所以这种引用是安全的; 但是只能引用基类成员. 若试图通过基类指针引用那些只在派生类中才有的成员,编译器会报告语法错 ...
- bzoj 1020-1029
1020 SHOI2008 安全的航线flight 这题的代码写了很久,主要是因为几何题的东西都忘得差不多了.除去写代码的2个小时,今晚又调了一晚上,终于AC了. 这题的做法还是很有参考价值的. 最简 ...
- 用overflow-y 解决web页面抖动问题
页面抖动(左右抖动)让人视觉上很不爽.. /** original : php攻城师 http://blog.csdn.net/phpgcs **/ 最开始我也以为是 layout 不一致的原因..后 ...
- Oracle单表的简单查询
Oracle单表的简单查询 查看表结构 desc emp; 查询所有列 Select * from emp; 查找所以部门编号(查指定的列) select deptnofrom emp; 查找编号不同 ...
- 微信或手机浏览器在线显示office文件(已測试ios、android)
近期开发微信企业号,发现微信andriod版内置浏览器在打开文件方面有问题,可是ios版没有问题.原因是ios版使用的是safari浏览器 支持文档直接打开.可是andriod版使用的是腾讯浏览器x5 ...
- JNI 详细解释
JNI事实上,Java Native Interface缩写,那是,java本地接口.它提供了许多API实现和Java和其它语言的通信(主要是C&C++). 或许不少人认为Java已经足够强大 ...
- service入门—电话监听器
1.继承Service类 2.在清单文件进行配置(因为Service属于四大组件之一) <service android:name=".PhoneStatusService" ...
- QNX 线程 调度策略 优先级 时钟频率 同步
/* * barrier1.c */ #include <stdio.h>#include <unistd.h>#include <stdlib.h>#includ ...
- cocos2d-x游戏开发(十五)游戏加载动画loading界面
个人原创,欢迎转载:http://blog.csdn.net/dawn_moon/article/details/11478885 这个资源加载的loading界面demo是在玩客网做逆转三国的时候随 ...
- IT大数据服务管理高级课程(IT服务,大数据,云计算,智能城市)
个人简历 金石先生是马克思主义中国化的研究学者,上海财经大学经济学和管理学硕士,中国民主建国会成员,中国特色社会主义人文科技管理哲学的理论奠基人之一.金石先生博学多才,对问题有独到见解.专于工作且乐于 ...