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 (搜索范围) 解题思路和方法的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  2. [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 ...

  3. 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 ...

  4. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  5. 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 ...

  6. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  7. [leetcode 34] search for a range

    1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  8. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  9. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

随机推荐

  1. SimpleAdapter

    1.视图 1)主视图 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  2. [转]CentOS下性能监测工具 dstat

    原文链接:http://www.bkjia.com/Linuxjc/935113.html 参考链接:https://linux.cn/article-3215-1.html,http://lhfli ...

  3. SFTP上传下载(C#)

    sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh 不支持,所有采用Ssh.Net方式 需要依赖:Re ...

  4. 百度地图api基本用法

    首先 ,如果想调用百度地图api,你需要获取一个百度地图api的密钥. 申请密钥很简单,在百度地图api的首页就有相关链接,填写相关信息百度就会给你一个密钥了. 接下来,就是引入百度地图的api 关键 ...

  5. 异步的两种写法: async 与 BeginInvoke

    现在要实现异步只要用关键字async/await就可以轻松实现,在此之前需要用到委托/回调等一堆东西. 对一下是对比写法: class Program { delegate string SendMe ...

  6. hdu5338 ZZX and Permutations

    hdu5338 ZZX and Permutations 非原创,来自多校题解 不是自己写的,惭愧ing…… 留着以后自己参考…… lower_bound {1,2,4,5} 询问 2,返回的是 2 ...

  7. HDU1686——Oulipo

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  8. Spark SQL 源代码分析之 In-Memory Columnar Storage 之 in-memory query

    /** Spark SQL源代码分析系列文章*/ 前面讲到了Spark SQL In-Memory Columnar Storage的存储结构是基于列存储的. 那么基于以上存储结构,我们查询cache ...

  9. 跟我一起写 Makefile(一)

    跟我一起写 Makefile  陈皓 概述—— 什么是makefile?也许非常多Winodws的程序猿都不知道这个东西,由于那些Windows的IDE都为你做了这个工作,但我认为要作一个好的和pro ...

  10. Swift - iOS应用的国际化与本地化

    在Xcode中我们可以很方便的将APP适配各种本地化语言.苹果的框架已经帮我们把不同语言的数据分离开,包括图片,声音,视频,文档,用户界面文字(甚至代码中编写的用户界面文字),它们会被建立在同一个bu ...