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 ...
随机推荐
- 举例android项目中的string.xml出现这个The character reference must end with the ';' delimiter.错误提示的原因及解决办法
今天在一个android项目中的string.xml中写这样一个字符串时出现了下面这个错误提示: The reference to entity "说明" must end wit ...
- 用tomcat搭建web服务器
链接地址:http://www.blogjava.net/qingshow/archive/2010/01/17/309846.html qingshow “不积跬步无以至千里,不积小流无以成江海”. ...
- 解决打包时IsCmdBld.exe出错的问题
1.查看环境变量是否配置了 2.查看是否是使用administrator登陆的,要求使用administrator登陆否则可能会出现权限不足的现象
- 演练5-3:Contoso大学校园管理系统3
在前面的教程中,我们使用了一个简单的数据模型,包括三个数据实体.在这个教程汇中,我们将添加更多的实体和关系,按照特定的格式和验证规则等自定义数据模型. Contoso大学校园管理系统的数据模型如下. ...
- Android NumberPicker和DatePicker分割线颜色设置
NumberPicker /** * * 设置选择器的分割线颜色 * * @param numberPicker */ private void setDatePickerDividerColor(N ...
- python中打印文件名,行号,路径
print "I have a proble! And here is at Line: %s"%sys._getframe().f_lineno PDB,哈哈http://doc ...
- 基于visual Studio2013解决C语言竞赛题之1004平均值
题目 解决代码及点评 /************************************************************************/ /* 4. 编一个程序, ...
- UVALive 6584 Escape (Regionals 2013 >> Europe - Central)
题目 给出一棵树,每个节点有一个怪物或血药,遇到怪物必须打,打完后扣掉一定的血量. 一开始英雄的血量为\(0\),当血量小于\(0\)时就挂了. 给出英雄的起点和终点,问能否成功到达终点. 算法 这题 ...
- Java数组与泛型
Java中不能创建泛型数组,例如不能这样写:[java] view plaincopyArrayList<String>[] as = new ArrayList<String> ...
- Java进阶01 String类
链接地址:http://www.cnblogs.com/vamei/archive/2013/04/08/3000914.html 作者:Vamei 出处:http://www.cnblogs.com ...