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。

  1. 如果A[l] < A[mid],说明mid以左有序且都小于mid,如下图所示:这种情况下如果target在l和mid之间,那么需要把r重新置为mid;其他情况都需要到mid右端继续搜索。

2.如果A[l] >= A[mid], 说明mid以右有序且都大于mid,如下图所示,如果target在mid和r之间,那么需要把l重新置为mid;其他情况都需要到mid左端继续搜索。

当l + 1 = r的时候,只要检查l和r所指向的元素是否等于target即可。

代码如下:

 public class Solution {
public int search(int[] A, int target) {
int l = 0;
int r = A.length - 1; while(l + 1< r){
int mid = l + (r-l)/2;
if(A[mid] == target)
return mid;
if(A[l]< A[mid] ){
if(A[mid] >= target && A[l] <= target)
r = mid;
else {
l = mid;
}
}
else {
if(target >= A[mid] && target <= A[r])
l = mid;
else {
r = mid;
}
}
} if(target == A[l])
return l;
if(target == A[r])
return r;
return -1;
}
}

【leetcode刷题笔记】Search in Rotated Sorted Array的更多相关文章

  1. 刷题33. Search in Rotated Sorted Array

    一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...

  2. LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...

  3. LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  4. LeetCode(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 m ...

  5. 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)

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

  6. 【leetcode刷题笔记】Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  7. leetcode个人题解——#33 Search in Rotated Sorted Array

    思路:每次取中间元素,一定有一半有序,另一半部分有序,有序的部分进行二分查找,部分有序的部分递归继续处理. class Solution { public: ; int middleSearch(in ...

  8. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  9. LeetCode: Search in Rotated Sorted Array 解题报告

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

  10. [LeetCode] Search in Rotated Sorted Array II [36]

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

随机推荐

  1. Windows7 配置两个版本的java环境,可自由切换

    1. 准备工作 下载jdk: jdk1.7[http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads ...

  2. 【BZOJ2728】[HNOI2012]与非 并查集+数位DP

    [BZOJ2728][HNOI2012]与非 Description Input 输入文件第一行是用空格隔开的四个正整数N,K,L和R,接下来的一行是N个非负整数A1,A2……AN,其含义如上所述.  ...

  3. zookeepeer ID生成器 (一)

    目录 写在前面 1.1. ZK 的分布式命名服务 1.1.1. 分布式 ID 生成器的类型 UUID方案 1.1.2. ZK生成分布式ID 写在最后 疯狂创客圈 亿级流量 高并发IM 实战 系列 疯狂 ...

  4. appium报'Command 'D\:\\android-sdk-windows\\platform-tools\\adb.exe -P 5037 -s “adb device” shell pm clear appPackage' exited with code 1'

    解决方法:是因为手机开发者模式没有允许USB调试(安全模式),打开即可

  5. 【python】-- 文件操作

    一.概述 我们工作中需要经常操作文件,下面就讲讲如何用Python操作文件 1.文件操作的流程: 打开文件,得到文件句柄赋值给一个变量 通过文件句柄,对文件进行操作 关闭文件 #获取文件句柄 f = ...

  6. Day 1 :成功完成注册

        今天成功完成了cnblogs的注册,之后会在这里开业咯!记录下此刻时间

  7. Zabbix-Agent 客户端安装配置

    1.安装Zabbix官方的yum源 [root@crazy-acong ~]# rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zab ...

  8. Django 视图之CBV

    CBV 所谓的CBV(class base view) 在视图里面,用类的方式来写逻辑 那么对于FBV,CBV有什么优势? CBV(class base views) 就是在视图里使用类处理请求. P ...

  9. Bootstrap第2天

    Bootstrap第2天 解决IE低版本不支持HTML5元素的方法 CSS全局样式--栅格系统 1.栅格系统介绍     Bootstrap提供了一套响应式.移动设备优先的流式的栅格系统.     B ...

  10. android 自定义progressbar 样式

    在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml <layer-list xmlns:android="http://sche ...