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 its index, otherwise return -1.

You may assume no duplicate exists in the array.

设置 low=0  high=len-1

bsearch(A,low,high)=bsearch(A,low,mid-1)||bsearch(A,mid+1,high)   (A[mid]!=target)

mid                 (A[mid]==target)

这是传统的思路:

我们经过观察发现,只要 A[high]>=A[low]  数组一定是递增的,则选用二分查找

如果不是,则分割 ,使用递归,一定能分割成数组满足第一种状态,

比如,上边的例子我们查找  5,

mid=3,A[mid]=7;分割成(4,5,6)和(0,1,2)连个尾巴都是大于首部,所以直接会被二分查找。

 public class Solution {
public int search(int[] A, int target) {
int len=A.length;
if(len==0) return -1;
return bsearch(A,target,0,len-1); } public int bsearch(int A[],int target,int low,int high)
{
if(low>high) return -1;
int idx=-1;
if(A[low]<=A[high]) //it must be YouXu,so binary search {
while(low<=high)
{
int mid=(low+high)>>1;
if(A[mid]==target)
{
idx=mid;
return idx;
}
else if(A[mid]>target)
{
high=mid-1;
}
else low=mid+1;
} }
else
{
int mid=(low+high)>>1;
if(A[mid]==target)
{
idx=mid;
return idx; }
else
{
idx=bsearch(A,target,low,mid-1);
if(idx==-1)
{
idx=bsearch(A,target,mid+1,high); } } } return idx; }
}

leetcode旋转数组查找 二分查找的变形的更多相关文章

  1. 【转】Java实现折半查找(二分查找)的递归和非递归算法

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://wintys.blog.51cto.com/425414/94051 Java二分 ...

  2. PHP实现文本快速查找 - 二分查找

    PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...

  3. [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找

    题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...

  4. LeetCode刷题总结-二分查找和贪心法篇

    本文介绍LeetCode上有关二分查找和贪心法的算法题,推荐刷题总数为16道.具体考点归纳如下: 一.二分查找 1.数学问题 题号:29. 两数相除,难度中等 题号:668. 乘法表中第k小的数,难度 ...

  5. PHP-----二维数组和二分查找

    二维数组由行和列组成.由arr[$i][$j]表示,先后表示行和列,类似于坐标点. 打印二维数组-----通过两次遍历,第一次遍历每一行,第二次遍历每一行的具体元素,并且通过使用count($arr[ ...

  6. java 13-1 数组高级二分查找

    查找: 1.基本查找:数组元素无序(从头找到尾) 2.二分查找(折半查找):数组元素有序 pS:数组的元素必须有顺序,从小到大或者从大到小.以下的分析是从小到大的数组 二分查找分析: A:先对数组进行 ...

  7. Java数据结构和算法总结-数组、二分查找

    前言:在平时开发中数组几乎是最基本也是最常用的数据类型,相比链表.二叉树等又简单很多,所以在学习数据和算法时用数组来作为一个起点再合适不过了.本篇博文的所有代码已上传 github ,对应工程的 ar ...

  8. [c/c++] programming之路(15)、多维数组和二分查找法,小外挂

    一.多维数组 #include<stdio.h> #include<stdlib.h> void main(){ ][]; int i,j; ; i < ; i++) { ...

  9. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

随机推荐

  1. Linux下通过软链接转移mysql目录,解决分区空间不足(转)

    http://darwinclub.info/wp/?p=454(转) 当存放数据库分区的空间不足时,可以采取对数据库目录进行迁移的方法,具体步骤如下:1.先关闭数据库mysqladmin -p sh ...

  2. windows平台 culture name 详细列表

    点击打开链接http://msdn.microsoft.com/zh-cn/goglobal/bb896001.aspx LCID Culture Identifier Culture Name Lo ...

  3. 简单的doc命令

    cd 切换目录 dir 显示目录列表 mkdir 创建目录(mkdir) rmdir 删除空目录(rmdir test) rmdir  /s 删除非空目录(rmdir test /s) echo 创建 ...

  4. CSS定位机制

    CSS中,存在3种定位机制:标准文档流(Normal flow) * 特点:从上到下,从左导游,输出文档内容 * 由块级元素和行级元素组成 浮动(Floats) * 能够实现横向多列布局 * 设置了浮 ...

  5. jQuery iframe 自适应高宽度

    Html <iframe id="你的id" src="你要嵌入的页面" scrolling="no" frameborder=&qu ...

  6. ES6 语法简介

    参考: http://es6.ruanyifeng.com/ 总结学习 JavaScript语言下一代标准,2015年6月正式发布. 1.let和const命令 let用作变量声明,只在代码块内有效 ...

  7. Gitlab服务器搭建(For fedora23)

    1. Install and configure the necessary dependencies sudo yum install curl policycoreutils openssh-se ...

  8. [BUGFIX]__import_pywin32_system_module__

    import_pywin32_system_module 修复方法: 编辑 X:/Python27/Lib/site-packages/win32/lib/pywintypes.py 第114行 if ...

  9. 4071: [Apio2015]巴邻旁之桥

    Description 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域 A 和区域 B. 每一块区域沿着河岸都建了恰好 1000000001 栋的建筑,每条岸边的建筑都从 0 编号到 10000 ...

  10. iOS开发者计划(转)

    苹果对软件和开发者的管理十分严格,你只有加入了Apple Developer计划之后,才能将你的软件放到真机上运行或者发布到App Store上去.这种方法看似麻烦,但是却有效的解决了盗版和劣质软件充 ...