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.

ps:这题和剑指offter的题目类似。一次AC

思路,由于没有重复数字,

1.我们先找出最大值在哪个位置。

2.然后拿target和A[0]做比较,若target>A[0],则在0~max_loc之间来二分查找数组;若target<A[0],则在max_loc+1~n-1来查找数组。

代码:

class Solution {
public:
int binarySearch(int start,int end,int A[],int target){
int l=start;int r=end;int mid=;
while(l<=r){
mid=(l+r)/;
if(A[mid]>target) r=mid-;
else if(A[mid]<target) l=mid+;
else return mid;
}
return -;
}
int findMaxValueLoction(int A[],int n){
if(A[n-]>A[]) return n-;
int l=;int r=n-;int mid=;
while(l<=r){
mid=(l+r)/;
if(mid==) return mid;
else if(mid!=&&A[mid]>A[]&&A[mid]>A[mid-]&&A[mid]<A[mid+]){
l=mid+;
}
else if(mid!=&&A[mid]<A[]&&A[mid]>A[mid-]&&A[mid]<A[mid+]){
r=mid-;
}
else if(mid!=&&A[mid]>A[mid-]&&A[mid]>A[mid+]){
return mid;
}else{
return mid-;
}
}
return -;
}
int search(int A[], int n, int target) {
if(A==NULL||n==) return -;
int max_loc=findMaxValueLoction(A,n);
if(target>A[])
return binarySearch(,max_loc,A,target);
else if(target<A[])
return binarySearch(max_loc+,n-,A,target);
else
return ;
}
};

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

  1. 33. Search in Rotated Sorted Array(二分查找)

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

  2. Leetcode 153. Find Minimum 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

    Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...

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

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

  5. leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法

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

  6. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

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

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

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

  8. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II

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

  9. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  10. Search in Rotated Sorted Array I II

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

随机推荐

  1. laravel如何查找门脸及注入类方法

    门脸模式 通过 config/app.php 查看别名对应类名 Illuminate\Support\Facades\Log ,查看 LoggerInterface 类文件,得:命名空间+接口名 Ps ...

  2. Java JDK装配置

     1- 介绍 本文章介绍JAVA开发环境安装是基于:  Java8(JDK8) 2- 下载JDK http://www.oracle.com/technetwork/java/javase/dow ...

  3. 使用JavaScript给对象修改注册监听器

    我们在开发一些大型前端项目时,会遇到这样一种情况,某个变量上有个字段.我们想知道是哪一段程序修改了这个变量上的字段.比如全局变量window上我们自定义了一个新字段_name,我们想知道到底有哪些程序 ...

  4. JAVA中等待所有线程都执行结束(转2)

    场景: package com.java4all.mypoint; import java.util.concurrent.CountDownLatch; public class ThreadTes ...

  5. [LOJ] 分块九题 8

    区间查询数值+整体赋值 维护tag代表整个区间被赋成了tag[i] 用pushdown操作,而不是修改了再check. 不压缩代码了,调起来心累,长点有啥不好. //Stay foolish,stay ...

  6. 如何用纯 CSS 创作 404 文字变形为 NON 文字的交互特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ZoxjXm 可交互视频教 ...

  7. 条款8:别让异常逃离析构函数(prevent exception from leaving destructors)

    NOTE: 1.析构函数绝对不要吐出异常.如果一个被析构函数调用的函数可能抛出异常,析构函数应该扑捉任何异常,然后吞下他们(不传播)或结束程序. 2.如果客户需要对某个操作函数运行期间抛出的异常做出反 ...

  8. Django之学员管理

    Django之学员管理 实现-------在前端页面提交的数据,后端可直接写入数据库.在页面实现操作数据库的增删改查. 数据表设计:(三个角色四张表) 班级表: id title 1 花果山国小一年级 ...

  9. Django-Ajax组件

    Ajax Ajax简介 AJAX(Asynchronous Javascript And XML),翻译成中文为"异步Javascript和XML".即使用Javascript语言 ...

  10. sql中Distinct&Count的用法

    Distinct作用:消除重复的数值 1.如: select id from T1 select distinct id from T1 二者的检索效果如下: distinct可以用来修饰多列,如: ...