https://oj.leetcode.com/problems/search-for-a-range/就是一个二分查找,没事练练手

  1. public class Solution {
  2. public int[] searchRange(int[] A, int target) {
  3. int a[]=new int[2];
  4. int ans=bSearch(A,target);
  5. if(ans==-1)
  6. {
  7. a[0]=-1;
  8. a[1]=-1;
  9. return a;
  10.  
  11. }
  12. else
  13. {
  14. int beg=ans;
  15. int end=ans;
  16. while(beg>=0&&(A[beg]==A[ans]))
  17. {
  18. beg--;
  19.  
  20. }
  21. while(end<A.length&&(A[end]==A[ans]))
  22. {
  23. end++;
  24. }
  25. a[0]=beg+1;
  26. a[1]=end-1;
  27. return a;
  28.  
  29. }
  30.  
  31. }
  32. public int bSearch(int[] A,int target)
  33. {
  34. int low=0;
  35. int high=A.length-1;
  36. while(low<=high)
  37. {
  38. int mid =(low+high)/2;
  39. if(A[mid]==target)
  40. {
  41. return mid;
  42.  
  43. }
  44. else if(A[mid]>target)
  45. {
  46.  
  47. high=mid-1;
  48.  
  49. }
  50. else
  51. {
  52. low=mid+1;
  53. }
  54.  
  55. }
  56.  
  57. return -1;
  58. }
  59.  
  60. }

2.另外一种二分查找 beg=0 end=len;  end指向的是最后一个元素的后面,

  1. public class Solution {
  2. public int[] searchRange(int[] A, int target) {
  3. int a[]=new int[2];
  4. int ans=bSearch(A,target);
  5. if(ans==-1)
  6. {
  7. a[0]=-1;
  8. a[1]=-1;
  9. return a;
  10.  
  11. }
  12. else
  13. {
  14. int beg=ans;
  15. int end=ans;
  16. while(beg>=0&&(A[beg]==A[ans]))
  17. {
  18. beg--;
  19.  
  20. }
  21. while(end<A.length&&(A[end]==A[ans]))
  22. {
  23. end++;
  24. }
  25. a[0]=beg+1;
  26. a[1]=end-1;
  27. return a;
  28.  
  29. }
  30.  
  31. }
  32. public int bSearch(int[] A,int target)
  33. {
  34. int low=0;
  35. int high=A.length;
  36. while(low<high) //low<high不能等于
  37. {
  38. int mid =(low+high)/2;
  39. if(A[mid]==target)
  40. {
  41. return mid;
  42.  
  43. }
  44. else if(A[mid]>target)
  45. {
  46.  
  47. high=mid;
  48.  
  49. }
  50. else
  51. {
  52. low=mid+1;
  53. }
  54.  
  55. }
  56.  
  57. return -1;
  58. }
  59.  
  60. }

3.第一种方法的递归方式

  1. public class Solution {
  2. public int[] searchRange(int[] A, int target) {
  3. int a[]=new int[2];
  4. int ans=bSearch(A,target,0,A.length);
  5. if(ans==-1)
  6. {
  7. a[0]=-1;
  8. a[1]=-1;
  9. return a;
  10.  
  11. }
  12. else
  13. {
  14. int beg=ans;
  15. int end=ans;
  16. while(beg>=0&&(A[beg]==A[ans]))
  17. {
  18. beg--;
  19.  
  20. }
  21. while(end<A.length&&(A[end]==A[ans]))
  22. {
  23. end++;
  24. }
  25. a[0]=beg+1;
  26. a[1]=end-1;
  27. return a;
  28.  
  29. }
  30.  
  31. }
  32. public int bSearch(int[] A,int target,int low,int high)
  33. {
  34.  
  35. while(low<high)
  36. {
  37. int mid =(low+high)/2;
  38. if(A[mid]==target)
  39. {
  40. return mid;
  41.  
  42. }
  43. else if(A[mid]>target)
  44. {
  45.  
  46. bSearch(A,target,low,mid);
  47.  
  48. }
  49. else
  50. {
  51. bSearch(A,target,mid,high);
  52. }
  53.  
  54. }
  55.  
  56. return -1;
  57. }
  58.  
  59. }

leetcode 二分查找的更多相关文章

  1. leetcode二分查找问题整理

    自从做完leetcode上的三道关于二分查找的题后,我觉得它是比链表找环还恶心的题,首先能写出bugfree代码的人就不多,而且可以有各种变形,适合面试的时候不断挑战面试者,一个程序猿写代码解决问题的 ...

  2. [leetcode]二分查找总结

    Search for a Range 1.最简单的想法,用最普通的二分查找,找到target,然后向左右扩张,大量的重复的target,就会出现O(n)效率. class Solution { pub ...

  3. leetcode 二分查找 Search in Rotated Sorted ArrayII

    Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...

  4. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

  5. leetcode 二分查找 Search in Rotated Sorted Array

    Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...

  6. LeetCode 二分查找模板 II

    模板 #2: int binarySearch(vector<int>& nums, int target){ if(nums.size() == 0) return -1; in ...

  7. LeetCode 二分查找模板 I

    模板 #1: int binarySearch(vector<int>& nums, int target){ if(nums.size() == 0) return -1; in ...

  8. LeetCode 二分查找模板 III

    模板 #3: int binarySearch(vector<int>& nums, int target){ if (nums.size() == 0) return -1; i ...

  9. leetcode二分查找相关

    目录 33/81搜索旋转排序数组 34在排序数组中查找元素的第一个和最后一个位置 35搜索插入位置 74搜索二维矩阵 300最长上升子序列,354俄罗斯套娃信封问题 33/81搜索旋转排序数组 假设按 ...

随机推荐

  1. 九度OJ 1370 数组中出现次数超过一半的数字

    题目地址:http://ac.jobdu.com/problem.php?pid=1370 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2 ...

  2. Null Pointer --设计模式

    在Joshua Bloch很有名的一本书<Effective in java>中建议不要在代码中返回空的collection/map/array,就像下面的代码一样: public Lis ...

  3. DIV+CSS 网页布局之:两列布局

    1.宽度自适应两列布局 两列布局可以使用浮动来完成,左列设置左浮动,右列设置右浮动,这样就省的再设置外边距了. 当元素使用了浮动之后,会对周围的元素造成影响,那么就需要清除浮动,通常使用两种方法.可以 ...

  4. get值乱码(gbk编码浏览器造成)

     $condition = urldecode($condition); 即可

  5. lucene解决全文检索word2003,word2007的办法

    在上一篇文章中 ,lucene只能全文检索word2003,无法检索2007,并且只能加载部分内容,无法加载全文内容.为解决此问题,找到了如下方法 POI 读取word (word 2003 和 wo ...

  6. 快速生成json实体类

    读取一个json文件,并与实体相对应: static void Main(string[] args) { string json = ""; FileStream fs = ne ...

  7. cocos2d-x笔记2: 编译到安卓的步骤与注意事项

    博客地址: www.cnblogs.com/wolfred7464/ 不得不说,真心复杂,本篇博客总结的基本是最简最直接的步骤了,不用Cygwin和Ant的,当然用也可以... 以下用 %PROJEC ...

  8. 如何跳到系统设置界面-b

    NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"]; if ([[UIApplication sharedApplicati ...

  9. Code First 指定外键名称

    指定类外键有注释(DataAnnotation)和FluentAPI两种方式, 目前掌握的知识我们先把DataAnnotation可用的四种方式总结如下 第一种方法: //1-指定导航属性,会自动生成 ...

  10. 李洪强iOS开发Swift篇—09_属性

    李洪强iOS开发Swift篇—09_属性 一.类的定义 Swift与Objective-C定义类的区别 Objective-C:一般需要2个文件,1个.h声明文件和1个.m实现文件 Swift:只需要 ...