题目:

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.

三个招:前两个很简单,巧妙地是第三个。

//方法一:分批次二分查找 ;查找出分割点需要时间O(n),总时间复杂度 O(n)
int BinaryResearch(int A[],int low,int high,int target)
{
int l = low;
int h = high;
while(l<=h)
{
int mid = (int)((h+l)/2);
if(A[mid]==target) return mid;
else if(A[mid]<target) l = mid+1;
else h = mid-1;
}
return A[h];
}
int search1(int A[], int n, int target) { int index = 0;
for(int i = 0;i<n-1;i++)
{
if(A[i]>A[i+1])
{
index = i;
break;
}
}
int a = BinaryResearch(A,0,index,target);
int b = BinaryResearch(A,index+1,n-1,target);
if(a==-1&&b==-1)
return -1;
else
return a==-1?b:a;
}
int search2(int A[], int n, int target) {
//顺序查找 ,O(n)
int index = -1;
for(int i = 0;i<n;i++)
{
if(A[i]==target)
{
index = i;
}}
return index;
}
//完全的二分查找,O(logn)
int search3(int A[], int n, int target) {
int left = 0;
int right = n-1;
while(left<=right)
{
int mid = (int)((left + right)/2);
if(A[mid] == target) return mid;
if(A[left]<A[mid])//A[mid]在前半部分
{
if(target<A[mid]&&target>=A[left])
right = mid-1;
else left = mid+1;
}
else if(A[left]>A[mid])//A[mid]位于后半段
{
if(target>A[mid]&&target<=A[right])
left = mid+1;
else
right = mid-1;
}
else left++;
}
return -1; }

LeetCode_Search in Rotated Sorted Array的更多相关文章

  1. leetcode_Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  2. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  6. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

  7. LintCode Find Minimum In Rotated Sorted Array

    1. 画图, 直观. 2. 讨论数组为空或者个数为零. 3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者. public class Solution { /** * @par ...

  8. LeetCode-Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  9. Leetcode Find Minimum in Rotated Sorted Array I and II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. 服务器端cs文件

    服务器端向mysql数据库写数据 using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Colle ...

  2. hdu3948(后缀数组)

    题意:给一串字符,需要你求不相同的回文子串个数....... 同ural1297,链接:http://www.cnblogs.com/ziyi--caolu/archive/2013/06/09/31 ...

  3. Ecshop安装的坑,建议不要使用!

    最近因为工作的需要,安装了下ecshop,这个曾经的火爆开源程序,现在也呈现出疲态. 1.请看官方的运行环境推荐: 服务器端运行环境推荐·php版本5.0以上5.3以下的版本(推荐使用5.2系列版本) ...

  4. 使用python对mysql主从进行监控,并调用钉钉发送报警信息

    1.编写python的监控脚本 A.通过获取mysql库中的状态值来判断这个mysql主从状态是否正常 B.进行两个状态值的判断 C.进行调取钉钉机器人,发送消息 2.设置定时任务进行脚本运行 cro ...

  5. [Application]Ctrl+C终止程序代码

    代码如下: #include <stdio.h> #include <stdlib.h> #include <iostream> #include <sign ...

  6. 使用模板方法模式简化JDBC操作

    在使用JDBC时,会重复的写很多重复的代码,例如 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; S ...

  7. TCP状态图

    TCP建立关闭连接状态图 MSL:Max segment lifetime最大段存活时间. MSL在RFC1122中规定为两分钟,但是各个操作系统的实现不同,在linux上一般配置MSL???? 处于 ...

  8. Nginx服务器防止被压力测试

    最近经常用人帮忙做压力测试,用webbech或者ab等一些工具模拟并发压服务器,若服务器没有限制连接数或带宽,服务器很容易被压跨.cat nginx.conf…http {…limit_conn_zo ...

  9. addLoadEvent

    function addLoadEvent(func){ var oldOnload = window.onload; if(typeof(window.onload) != 'function'){ ...

  10. 学习:100个高质量Java开发者博客

    谷歌关键字搜索:100个高质量Java开发者博客. Java开发牛人十大必备网站.