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.

思路:

  给你一个已排序好但移位了的数组,找到特定的数。画一幅图就很容易理解了。一个被旋转的有序数组A[1..n],假定转折点是A[k],那么A[k+1] < A[k+2] < ... < A[n] < A[1] < A[2] < ... < A[k]

  可以用二分查找稍微改点型:虽然我们不知道转折点k在哪,但是我们还是可以通过比较A[mid]与A[start],A[end]来确定要找的目标书target是在A[start,...,mid]中,还是在A[mid+1,...,end]  所以时间复杂度还是lg(n)

代码:

Runtime: 12 ms

class Solution{
public:
int search(int A[], int n, int target) {
if (n <= )
return -;
if (n == )
return *A == target ? : -; int begin = , end = n, mid = (begin + end) / ; if (A[begin] <= A[mid-]) {
if (target >= A[begin] && target <= A[mid-]) {
auto it = lower_bound(A, A + mid, target);
if (*it == target)
return it - A;
else
return -;
}
int res = search(A + mid, end - mid, target);
return res == - ? - : mid + res;
}
else {
if (target >= A[mid] && target <= A[end-]) {
auto it = lower_bound(A+mid, A+end, target);
if (*it == target)
return it - A;
else
return -;
}
int res = search(A + begin, mid - begin, target);
return res == - ? - : begin + res;
}
}
};

leetcode problem 33 -- Search in Rotated Sorted Array的更多相关文章

  1. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  2. LeetCode题解33.Search in Rotated Sorted Array

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  3. 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)

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

  4. 【一天一道LeetCode】#33. Search in Rotated Sorted Array

    一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...

  5. LeetCode OJ 33. 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】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【Leetcode】33. Search in Rotated Sorted Array

    Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...

  8. LeetCode:33. Search in Rotated Sorted Array(Medium)

    1. 原题链接 https://leetcode.com/problems/search-in-rotated-sorted-array/description/ 2. 题目要求 给定一个按升序排列的 ...

  9. Python 解LeetCode:33. Search in Rotated Sorted Array

    题目描述:在一个旋转数组中查找给定的值,其中旋转数组中不含重复值: 思路: 第一遍二分遍历,找到数组中最小值的索引: 第二遍分别对最小值左右两边的数组进行二分查找: class Solution(ob ...

随机推荐

  1. PowerShell查询sql server

    function Invoke-SQL1: function Invoke-SQL { param( [string] $DBServer, [string] $Database, [string] ...

  2. 从零开始学android开发-查看sqlite数据库

    C:\Users\Administrator>cd E:\ProSoft\adt-bundle-windows-x86-20140321\sdk\platform-tools

  3. iOS开发——屏幕适配篇&Masonry详解

    Masonry详解 前言 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-ip ...

  4. careercup-中等难题 17.2

    17.2 设计一个算法,判断玩家是否赢了井字游戏. 解法: 假设这个检查某人是否赢得了井字游戏的函数为HasWon,那么我们第一步要向面试官确认, 这个函数是只调用一次,还是要多次频繁调用.如果是多次 ...

  5. 文件和目录之access函数

    本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. 当用open函数打开一个文件时,内核以进程的有 ...

  6. html笔记 横向两列布局

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Swift数据类型

    1.Swift中常用数据类型:首字母大写 Int.Float.Double.Bool.Character.String Array.Dictionary.元组类型(Tuple).可选类型Optiona ...

  8. nmblookup

    域网内可以通过下述命令来根据ip地址查询其他主机名(Linux) 使用nmblookup -A ip命令查询 [admin@v015213 ~/lpmall]$ nmblookup -A 10.19. ...

  9. PHP Fatal error: Cannot pass parameter 2 by reference

    PHP Fatal error:  Cannot pass parameter 2 by reference in 这个错误的意思是:不能按引用传递第2个参数 我的理解是: 方法的第2个参数 需要传递 ...

  10. Burosuite抓包Sqlmap学习Sql注入

    在sqlmap中加入--proyxy参数: --proxy "http://127.0.0.1:8080" 如下图所示: 回车以后sqlmap会自动抓到数据包: 我们选择向前(fo ...