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

 public class Solution {
public int[] searchRange(int[] A, int target) {
int a[]=new int[2];
int ans=bSearch(A,target);
if(ans==-1)
{
a[0]=-1;
a[1]=-1;
return a; }
else
{
int beg=ans;
int end=ans;
while(beg>=0&&(A[beg]==A[ans]))
{
beg--; }
while(end<A.length&&(A[end]==A[ans]))
{
end++;
}
a[0]=beg+1;
a[1]=end-1;
return a; } }
public int bSearch(int[] A,int target)
{
int low=0;
int high=A.length-1;
while(low<=high)
{
int mid =(low+high)/2;
if(A[mid]==target)
{
return mid; }
else if(A[mid]>target)
{ high=mid-1; }
else
{
low=mid+1;
} } return -1;
} }

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

 public class Solution {
public int[] searchRange(int[] A, int target) {
int a[]=new int[2];
int ans=bSearch(A,target);
if(ans==-1)
{
a[0]=-1;
a[1]=-1;
return a; }
else
{
int beg=ans;
int end=ans;
while(beg>=0&&(A[beg]==A[ans]))
{
beg--; }
while(end<A.length&&(A[end]==A[ans]))
{
end++;
}
a[0]=beg+1;
a[1]=end-1;
return a; } }
public int bSearch(int[] A,int target)
{
int low=0;
int high=A.length;
while(low<high) //low<high不能等于
{
int mid =(low+high)/2;
if(A[mid]==target)
{
return mid; }
else if(A[mid]>target)
{ high=mid; }
else
{
low=mid+1;
} } return -1;
} }

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

 public class Solution {
public int[] searchRange(int[] A, int target) {
int a[]=new int[2];
int ans=bSearch(A,target,0,A.length);
if(ans==-1)
{
a[0]=-1;
a[1]=-1;
return a; }
else
{
int beg=ans;
int end=ans;
while(beg>=0&&(A[beg]==A[ans]))
{
beg--; }
while(end<A.length&&(A[end]==A[ans]))
{
end++;
}
a[0]=beg+1;
a[1]=end-1;
return a; } }
public int bSearch(int[] A,int target,int low,int high)
{ while(low<high)
{
int mid =(low+high)/2;
if(A[mid]==target)
{
return mid; }
else if(A[mid]>target)
{ bSearch(A,target,low,mid); }
else
{
bSearch(A,target,mid,high);
} } return -1;
} }

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. c++primer复习(六)—面向对象编程

    1 C++中,通过基类的引用(或指针)调用虚函数时,发生动态绑定,两个条件(基类引用或指针.虚函数)缺一不可 虚函数的默认实参将发生静态绑定 2 继承层次的根类一般都需要定义虚析构函数 3 任意非st ...

  2. c++ primer复习(四)

    1 标准库容器 顺序容器:vector.list.deque 容器适配器:stack.queue.priority_queue 2 容器元素类型约束: 容器元素类型必须支持复制和赋值,因为容器存放的都 ...

  3. (转 部分修改) IOS 手势密码(简单版)

    // // Created by wangtouwang on 15/4/7. // Copyright (c) 2015年 wangtouwang. All rights reserved. // ...

  4. 关于Navicat导入MySQL数据库遇到的一些问题

    今天本想将之前的一个数据库easy.sql用图形化工具Navicat导入的,开始是用“运行SQL文件”导入,结果是“queries successfully”啥的,去查是否导表成功,一看并没有. 结果 ...

  5. C#控件命名规范

    文档名称: C#控件命名规范 撰写作者: codefly 版本编号: V1.1 C#控件命名规范 一.Data Control 类型 前缀 示例 AccessDataSource ads adsPub ...

  6. 数据库创建&数据表创建

    --第2_1题创建数据库 create database Student201038897041 on primary (name='student1', filename='F:\coures\SQ ...

  7. skymvc网站测试之mysql数据生成

    skymvc网站测试之mysql数据生成 使用方法: 删除数据 /index.php?m=test_mysql&a=autoDelete 重置自增ID /index.php?m=test_my ...

  8. [python]字符串方法

    字符串的方法及注释 字符串的方法及注释             capitalize()   把字符串的第一个字符改为大写   casefold()   把整个字符串的所有字符改为小写   cente ...

  9. 隐私:网民最常用密码MD5解密

    国内知名网络安全商瑞星公司曾发布过一项针对密码强度的专业研究报告,这项研究中列举了中国网民和美国网民最常用的密码集.研究表明,全球互联网大部分用户在密码使用中都存在着种种疏漏,一些极其简单的密码被广泛 ...

  10. .net source code

    .NET 类库的强大让我们很轻松的解决常见问题,作为一个好专研的程序员,为了更上一层楼,研究CLR的基础类库实现是快速稳定的捷径. 一般场景下,采用 Reflector可以反射出.NET 的部分实现出 ...