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.

思路:二分搜索,每次去掉一半的错误选项。

注意,每次 l = m + 1, r = m - 1 防止无限循环。

int search(int A[], int n, int target) {
int l = , r = n - ;
while(l <= r) //注意有等号
{
int m = (l + r) / ;
if(A[m] == target)
return m;
if(A[l] <= A[m] && A[m] <= A[r]) //顺序的
{
if(A[m] > target)
r = m - ;
else
l = m + ;
}
else if(A[l] >= A[m] && A[m] <= A[r]) //开头转到了左半部分
{
if(A[m] < target && target <= A[r]) //在右半部分
l = m + ;
else
r = m - ;
}
else //开头转到了右半部分
{
if(A[l] <= target && target <= A[m]) //在左半部分
r = m - ;
else
l = m + ;
}
}
return -;
}

大神简约版写法:去掉一半选项时的思路不同

int search(int A[], int n,int target) {
int lo = ;
int hi = n - ;
while (lo <= hi) {
int mid = (lo + hi) / ;
if (A[mid] == target) return mid; if (A[lo] <= A[mid]) {
if (target >= A[lo] && target < A[mid]) {
hi = mid - ;
} else {
lo = mid + ;
}
} else {
if (target > A[mid] && target <= A[hi]) {
lo = mid + ;
} else {
hi = mid - ;
}
}
}
return -;
}

【leetcode】Search in Rotated Sorted Array (hard)的更多相关文章

  1. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  2. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  3. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  4. 【leetcode】Search in Rotated Sorted Array II(middle)☆

    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】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 ...

  7. 【Leetcode】【Hard】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 ...

  8. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  9. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

随机推荐

  1. [译]Mongoose指南 - 验证

    开始前记住下面几点 Validation定义在SchemaType中 Validation是一个内部的中间件 当document要save前会发生验证 验证不会发生在空值上 除非对应的字段加上了 re ...

  2. Ruby中Block, Proc, 和Lambda

    Block Blocks就是存放一些可以被执行的代码的块,通常用do...end 或者 {}表示 例如: [1, 2, 3].each do |num| puts num end [1, 2, 3]. ...

  3. tc 146 2 RectangularGrid(数学推导)

    SRM 146 2 500RectangularGrid Problem Statement Given the width and height of a rectangular grid, ret ...

  4. 大数据之pig安装

    大数据之pig安装 1.下载 pig download 2. 解压安装 mapreduce模式安装: 1:设置HADOOP_HOME,如果pig所在节点不是集群中的节点,那就需要把集群中使用的hado ...

  5. 使用Carthage安装第三方Swift库

    http://blog.crazyphper.com/?p=3661 carthage update

  6. 破解TexturePacker加密资源

    http://blog.csdn.net/ynnmnm/article/details/38392795 最近我们要开一个新项目,UI与交互打算借鉴当前正火的<圣火英雄传>,程序开发为了和 ...

  7. Unity 用户手册用户指南二维纹理 (Texture 2D)

    http://www.58player.com/blog-2327-953.html 二维纹理 (Texture 2D) 纹理 (Textures) 使您的 网格 (Meshes).粒子 (Parti ...

  8. 分布式架构 Hadoop 2.7.X 安装和配置

    一.安装环境 硬件:虚拟机 操作系统:Ubuntu 14 32位 IP:59.77.132.28主机名:admin安装用户:root 二.安装JDK 安装JDK1.7或者以上版本.这里安装jdk1.7 ...

  9. mysql 数据库字符集的指定

    create database mydb default character set utf8 default collate utf8_general_ci;

  10. [20160804]synchronized

    class Timer{ private static int num; void add(String name){ //synchronized (this){ num++; try{ Threa ...