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

用二分查找去做

 public int search(int[] A, int target) {
int first = 0, last = A.length;
while (first != last) {
int mid = (first + last) / 2;
if (A[mid] == target)
return mid;
if (A[first] <= A[mid]) {
if (A[first] <= target && target < A[mid])
last = mid;
else
first = mid + 1;
} else {
if (A[mid] < target && target <= A[last - 1])
first = mid + 1;
else
last = mid;
}
}
return -1;
}

Search in Rotated Sorted Array II

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

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

允许重复,造成了如果A[m]>=A[l], 那么[l,m] 为递增序列的假设就不能成立了,比如[1,3,1,1,1]。

如果A[m]>=A[l] 不能确定递增,那就把它拆分成两个条件:
• 若A[m]>A[l],则区间[l,m] 一定递增
• 若A[m]==A[l] 确定不了,那就l++,往下看一步即可。

因此复杂度从O(lgn)变成了O(n)。具体说明参见剑指offer上查找旋转数组的最小元素。

 public boolean search(int[] A, int target) {
int first = 0, last = A.length;
while (first != last) {
int mid = (first + last) / 2;
if (A[mid] == target)
return true;
if (A[first] < A[mid]) {
if (A[first] <= target && target < A[mid])
last = mid;
else
first = mid + 1;
} else if (A[first] > A[mid]) {
if (A[mid] < target && target <= A[last - 1])
first = mid + 1;
else
last = mid;
} else {
first++;
}
}
return false;
}

Search in Rotated Sorted Array I II的更多相关文章

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

  2. Search in Rotated Sorted Array I&&II——二分法

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

  3. Search in Rotated Sorted Array (I, II) 解答

    Question Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 ...

  4. 【leetcode】Search in Rotated Sorted Array II

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

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

  6. [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路

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

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

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

  8. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  9. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

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

随机推荐

  1. SCSI接口图文详解

    目前存储设备的接口有五大类:IDE.SCSI.USB,并行口,串口,其中并行口与串口的速度非常慢,不提也罢,最主要的就是IDE,usb,SCSI.IDE(Integrated Drive Electr ...

  2. sh里没有多行注释,只能每一行加一个#号

    sh里没有多行注释,只能每一行加一个#号.只能像这样: #-------------------------------------------- # 这是一个自动打ipa的脚本,基于webfrogs ...

  3. css案例学习之按钮超链接

    效果 css实现 <html> <head> <title>按钮超链接</title> <style> a{ /* 统一设置所有样式 */ ...

  4. HDOJ-1009 FatMouse' Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=1009 # include <stdio.h> # include <algorithm> ...

  5. 04747_Java语言程序设计(一)_第1章_Java语言基础

    二进制0b开头 八进制0开头 十六进制0x开头 package com.jacky; public class Aserver { public static void main(String arg ...

  6. os x 10.10 測试版系统下载 swift语言学习资料下载

    http://pan.baidu.com/s/1eQ5oj1S               这是下载地址 ! 刚学完oc 就出了 swift!这----  只是还是非常高兴看了一点swith得东西感觉 ...

  7. mysql sql limit where having order

    SQL语句执行顺序及MySQL中limit的用法 . 分类: MySql2013-09-02 09:1315人阅读评论(0)收藏举报 写的顺序:select ... from... where.... ...

  8. ios8加入通知栏开始

    ios8加入通知栏开始 by 吴雪莹 以打开vpn设置为例: @IBAction func open(sender: AnyObject) { let context = self.extension ...

  9. php安装扩展模块(curl模块)

    php安装扩展模块的思路: 1,首先找到需要安装的扩展模块的目录.一般在/usr/local/php/ext目录下 但是有的模块php源码中不一定有,需要自己下载比如memcache.redis等. ...

  10. Android应用开发中webview上传文件的几种思路

    1. 常规方法,重写WebChromeClient 的 openFileChooser 方法 private class MyWebChromeClient extends WebChromeClie ...