[leetcode]二分查找总结

Search for a Range

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

 class Solution {
    public int[] searchRange(int[] A, int target) {
        int ans[]=new int[2];
      int a= bserch(A,target);
      if(a==-1) {ans[0]=-1;ans[1]=-1; return ans;}
     //left
     int b=a-1;
     while(b>=0&&A[b]==target) b--;
     ans[0]=b+1;
     b=a+1;

     while(b<=A.length-1&&A[b]==target) b++;
     ans[1]=b-1;
     return ans;

    }

    public  int bserch(int A[],int target)
    {
        int low=0;
        int end=A.length-1;
        while(low<=end)
        {
            int mid=(low+end)>>1;
            if(A[mid]==target) return mid;
            else if(A[mid]<target)  low=mid+1;
            else   end=mid-1;

        }

        return -1;
    }

}

2.二分查找加入第一个大的位置(上届),第一个大于等于它的位置(下界)

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

[leetcode]二分查找总结

时间: 2024-10-07 23:24:21

[leetcode]二分查找总结的相关文章

Leetcode 二分查找 Search a 2D Matrix

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search a 2D Matrix Total Accepted: 10871 Total Submissions: 35629 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in e

leetcode 二分查找 Search for a Range

Search for a Range Total Accepted: 21480 Total Submissions: 78454My Submissions 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

leetcode 二分查找 Search in Rotated Sorted Array

Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to s

leetcode 二分查找 Search in Rotated Sorted ArrayII

Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a functi

Leetcode 二分查找 Search Insert Position

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 Total Submissions: 41575 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be i

leetcode 二分查找 Sqrt(x)

Sqrt(x) Total Accepted: 26074 Total Submissions: 116517My Submissions Implement int sqrt(int x). Compute and return the square root of x. 题意:实现求方根 sqrt(x) 思路:二分法 对于一个数,它的方根不可能大于 x/2 + 1 问题转变为在[0,x/2 + 1]中找到一个数 v 使得 v * v == x 既然是在有序区间里找数,那么就可以用二分查找 注

二分查找的那些坑

听说很多人写不对二分查找,如果不好好总结一下,我大概也会是其中之一.. 历史上二分查找的bug 二分查找虽然原理很简单,实现起来却有很多的坑. <编程珠玑>的作者做实验发现90%的人写不对二分查找,然后亲手在该书里写下一个带 bug 的 binary search... 据说该 bug 在书里呆了二十年没人发现,而这本书还是一本人人交手称赞的好书. 然后 java 标准库里,一个和<编程珠玑>同样的 bug 在 2006 年才被发现.. 那这个 bug 是啥呢?是一个很好理解的问题

leetcode旋转数组查找 二分查找的变形

http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return it

leetcode 刷题之路 70 earch Insert Position 二分查找插入位置

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2