Question

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.

Solution

This problem is solved by binary search in O(log n) time.

Example

Above is an illustration of rotated sorted array. There are 2 possibilities for median position and 4 sub-situations of possible position of target:

1. red line (start < median)

  1.1 target < median && target >= start  -> [ start --- mid ]

  1.2 else                     -> [ mid --- end ]

2. green line (start > median)

  2.1 target > median && target <= end   -> [ mid --- end ]

  2.2 else                  -> [ start -- mid ]

 public class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length < 1)
return -1;
int start = 0, end = nums.length - 1, mid;
while (start + 1 < end) {
mid = (end - start) / 2 + start;
if (nums[mid] == target)
return mid;
if (nums[mid] > nums[start]) {
// Red line situation
if (nums[start] <= target && target < nums[mid]) {
end = mid;
} else {
start = mid;
}
} else {
// Green line situation
if (nums[end] >= target && target > nums[mid]) {
start = mid;
} else {
end = mid;
}
}
}
if (nums[start] == target)
return start;
if (nums[end] == target)
return end;
return -1;
}
}

Follow-up

What if duplicates are allowed?

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

Solution

If an array contains duplicate elements, we can't apply binary search.

For example, nums = {1, 1, 1, 1, 1, 1}, we find that start = mid = end. Under this circumstance, we cannot decide which next step should take.

So we can just use linear traverse to get result, time complexity O(n).

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 Suppose a sorted array is rotated at some pivot unknown to you before ...

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

  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. pythonchallenge学到的python内置函数整理

    第0关: 计算x的n次方: x**n 第一关: maketrans(from,to):建立一个翻译规则,将from翻译成to的翻译规则,因为要从from翻译成to,所以俩个参数的长度必须一致 tran ...

  2. PHP数据结构:栈、队列、堆、固定数组

    数据结构:栈 队列: 堆: 固定尺寸的数组:

  3. java 面试基础典型题及答案

    1.switch能否作用在byte.int.long.String? 答案:switch能作用在byte.int.enum常量, 补充:jdk7可以作用在String上 2.short s = 1; ...

  4. 如何改变Myeclipse编辑区背景色

    编辑窗口右键单击——>Preferences——>General加号——>Editors加号——>点Text Editors字样——>右下窗口选Backgroud col ...

  5. opencv 实现进度控制

    进度控制: #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\cxcore.h> ...

  6. linux下使用mutt发送带附件的邮件

    echo "hello"|mutt -s "world" -a hack.jpg -- name@address.com

  7. grub2手动引导ubuntu

    測试机OS为ubuntu 14.04.1 LTS x86_64 磁盘分区情况为: Filesystem     1K-blocks    Used Available Use% Mounted on ...

  8. Gson源码分析之Json结构抽象和注解使用

    github上的博客地址: http://chuyun923.github.io/blog/2015/01/06/gsonyuan-ma-fen-xi/ XML和Json作为最常用的两种网络传输格式而 ...

  9. 安装VMware Sphere ESXi 5.5

    安装VMware Sphere ESXi 5.5 1.准备 待安装ESXi 5.5的机器需要大于2GB以上内存,并且支持64位和虚拟化. 下载:VMware-VMvisor-Installer-5.5 ...

  10. 从反编译的角度去观察C#6.0

    1. 自动属性初始化 (Initializers for auto-properties) 1.1 C#6.0 之前的写法 public class FirstExperience { private ...