Search in Rotated Sorted Array

Total Accepted: 28132 Total
Submissions: 98526My Submissions

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.

题意:一个已经排序好的数组,被按某个位置旋转了一次。给定一个值target,在该旋转后的数组里查找该值。

思路:二分查找

难点在于确定往数组的哪一半段继续二分查找

设起点、中间点、终点分别为 start、middle、end (採用前闭后开的区间表示方法

假设target = A[middle] return middle

假设A[middle] >= A[start],则[start,middle)单调递增

1.假设target < A[middle] && target >= A[start],则 end = middle

2.start = middle + 1, otherwise

假设A[middle] < A[start]。则[middle,end)单调递增

1.假设target > A[middle] && target <= A[end - 1],则 start = middle + 1

2.end = middle, otherwise





复杂度:时间O(log n)。空间O(1)

int search(int A[], int n, int target){
int start = 0, end = n, middle ;
while(start < end){
middle = (start + end) / 2;
if(A[middle] == target) return middle;
if(A[middle] >= A[start]){
if(target >= A[start] && target < A[middle]){
end = middle;
}else{
start = middle + 1;
}
}else{
if(target > A[middle] && target <= A[end - 1]){
start = middle + 1;
}else{
end = middle;
}
}
}
return -1;
}

leetcode 二分查找 Search in Rotated Sorted Array的更多相关文章

  1. leetcode 二分查找 Search in Rotated Sorted ArrayII

    Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...

  2. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  3. leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

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

  4. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

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

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

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

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

  7. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

  8. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  9. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

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

随机推荐

  1. bootstrapValidator关于verbose需要优化的地方

    开发中需要用到bootstrapValidator的配置verbose:false,达到当前验证不通过不往下在验证的效果 问题: 当前字段需要remote验证时,此配置无效,原因在于remote是异步 ...

  2. [linux]多线程下载

    axel -n 10 -o /tmp/ http://soft.vpser.net/lnmp/lnmp0.7-full.tar.gz # 10是线程数

  3. serversql tinkphp

    public function iconv2utf8($Result) {         $Row=array();                    $key1=array_keys($Res ...

  4. BZOJ 3224: Tyvj 1728 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 22483  Solved: 10130[Submit][S ...

  5. HDU 1014 Uniform Generator(最大公约数,周期循环)

    #include<iostream> #include <cstdio> #include <cstring> using namespace std; int m ...

  6. CodeForces 669C

    链接:http://codeforces.com/problemset/problem/669/C http://www.cnblogs.com/Ash-ly/p/5443155.html 题意: 给 ...

  7. 22、Flask实战第22天:Flask信号

    Flask中的信号使用的是一个第三方插件blinker.通过pip list看一下是否安装,如果没有,则使用如下命令安装 pip install blinker 自定义信号 自定义信号分为3步: ①定 ...

  8. Gitlab运维

    安装Gitlab 新建 /etc/yum.repos.d/gitlab-ce.repo [gitlab-ce] name=gitlab-ce baseurl=http://mirrors.tuna.t ...

  9. Servlet 3.1 规范

    在线版目录 Servlet3.1规范翻译——前言 Servlet3.1规范翻译——概览 Servlet3.1规范翻译——Servlet Context Servlet3.1规范翻译——Response ...

  10. 5.10(java学习笔记)容器的同步控制与只读设置

    1.容器的同步控制 像我们平常使用的容器有些是不同步的即线程不安全,例如HashMap等,在多线程时可能出现并发问题. 而有些容器又是同步的,例如Hashtable. 有些时候我们需要将这些不同步的容 ...