题目:

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.

说明:

1)已排序数组查找采用二分查找

2)关键找到临界点

实现:

一、我的代码:

     

 class Solution {
public:
int search(int A[], int n, int target) {
if(n==||n==&&A[]!=target) return -;
if(A[]==target) return ;
int i=;
while(A[i-]<A[i]) i++; int pre=binary_search(A,,i,target);
int pos=binary_search(A,i,n-i,target);
return pre==-?pos:pre;
}
private:
int binary_search(int *B,int lo,int len,int goal)
{
int low=lo;
int high=lo+len-;
while(low<=high)
{
int middle=(low+high)/;
if(goal==B[middle])//找到,返回index
return middle;
else if(B[middle]<goal)//在右边
low=middle+;
else//在左边
high=middle-;
}
return -;//没有,返回-1
}
};

二、网上开源代码:

 class Solution {
public:
int search(int A[], int n, int target) {
int first = , last = n-;
while (first <= last)
{
const int mid = (first + last) / ;
if (A[mid] == target)
return mid;
if (A[first] <= A[mid])
{
if (A[first] <= target && target < A[mid])
last = mid-;
else
first = mid + ;
}
else
{
if (A[mid] < target && target <= A[last])
first = mid + ;
else
last = mid-;
}
}
return -;
}
};

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)的更多相关文章

  1. [LeetCode 题解] Search in Rotated Sorted Array

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目描述 Suppose an array ...

  2. [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  3. 【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 ...

  4. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

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

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

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

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

  7. Java for LeetCode 081 Search in Rotated Sorted Array II

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

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

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

  9. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  10. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

随机推荐

  1. MSSQL手札二 MSSQL的触发器

    触发器,就是在对表做DML操作的时候,触发一些其他的事件,触发器一般用在check约束更加复杂的约束上面,是一种特殊的存储过程,不可以被主动调用. 语法如下: CREATE TRIGGER trigg ...

  2. 【半原创】将js和css文件装入localStorage加速程序执行

    首先感谢某某作者写的文章:http://www.jb51.net/article/12793.htm 直接上代码,注意文件名为env.js 原理如下: 一次批量加要加载的文件存入数组,采用Ajax方式 ...

  3. Maven配置文件说明

    <projectxmlns="http://maven.apache.org/POM/4.0.0 "       xmlns:xsi="http://www.w3. ...

  4. [ASP.NET]更简单的方法:FormsAuthentication登录ReturnUrl使用绝对路径

    转自:http://www.cnblogs.com/dudu/p/formsauthentication-returnurl-absoluteuri.html [ASP.NET]更简单的方法:Form ...

  5. 自定义Template,向其中添加新的panel

    参考网站:https://www.devexpress.com/Support/Center/Example/Details/E2690 思路: 新建一个DefaultTemplate:       ...

  6. C# Hashtable 简述

    一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对,其中 ...

  7. C# 支付宝接口

    生成URL链接   1using System;  2using System.Data;  3using System.Configuration;  4using System.Collectio ...

  8. 采用Asp.Net的Forms身份验证时,非持久Cookie的过期时间会自动扩展

    问题描述 之前没有使用Forms身份验证时,如果在登陆过程中把HttpOnly的Cookie过期时间设为半个小时,总会收到很多用户的抱怨,说登陆一会就过期了. 所以总是会把Cookie过期时间设的长一 ...

  9. C#实现Combobox自动匹配字符

    不多说了,如图,应客户要求,下拉框中需要自动匹配字符,可能有些人一早就对此很熟,但相对于我还是首次使用,还是花了一点时间,现记录下来,也希望能帮助大家更好的理解. 首先要设定Combobox的Drop ...

  10. pjsip视频通信开发(上层应用)之EditText重写

    我们经常使用手机的打电话功能,当我们按键盘的时候,有一个地方显示我们按键的内容,当我们的手点击那个地方的时候,并没有弹出软件盘,所以我们再有数字键盘的时候,要屏蔽系统的软件盘. 我们分析一下,软件盘弹 ...