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.

Search in Rotated Sorted Array II,Find Minimum in Rotated Sorted Array,Find Minimum in Rotated Sorted Array II对照看

解法一:顺序查找 just a joke :D

class Solution {
public:
int search(int A[], int n, int target) {
for(int i = ; i < n; i ++)
{
if(A[i] == target)
return i;
}
return -;
}
};

解法二:二分查找

先用二分法找到最大元素,将数组切分成两个有序数组,再进行二分查找

class Solution {
public:
int search(int A[], int n, int target) {
if(n==)
return (target==A[])?:-; //find the maximum first
int low = ;
int high = n-;
while(low < high)
{
int mid = (low+high)/;
if(A[mid] < A[low])
high = mid-;
else if(A[mid] > A[low])
low = mid;
else
{//low+1==high
if(A[high]>A[low])
low = high;
break;
}
}
int ind = low;
//to here, low is the index of maximum
//0~ind, ind+1~n-1 are two sorted arrays
if(target >= A[])
{//first array: 0~ind
low = ;
high = ind;
while(low <= high)
{
int mid = (low+high)/;
if(target == A[mid])
return mid;
else if(target > A[mid])
low = mid+;
else
high = mid-;
}
return -;
}
else
{//second array: ind+1, n-1
low = ind+;
high = n-;
while(low <= high)
{
int mid = (low+high)/;
if(target == A[mid])
return mid;
else if(target > A[mid])
low = mid+;
else
high = mid-;
}
return -;
}
}
};

解法三:可处理重复元素的二分查找,即不断去掉low与high元素

class Solution {

public:
int search(int A[], int n, int target) {
int low = ;
int high = n-;
while (low <= high)
{
int mid = (low+high)/;
if(A[mid] == target)
return mid;
if (A[low] < A[mid])
{
if(A[low] <= target && target < A[mid])
//binary search in sorted A[low~mid-1]
high = mid - ;
else
//subproblem from low to high
low = mid + ;
}
else if(A[mid] < A[high])
{
if(A[mid] < target && target <= A[high])
//binary search in sorted A[mid+1~high]
low = mid + ;
else
//subproblem from low to mid-1
high = mid - ;
}
else if(A[low] == A[mid])
low += ; //A[low]==A[mid] is not the target, so remove it
else if(A[mid] == A[high])
high -= ; //A[high]==A[mid] is not the target, so remove it
}
return -;
}
};

解法四:

二分查找,先对mid元素处于前半段还是后半段分情况讨论,再对target元素处于前半段还是后半段分情况讨论。

class Solution {
public:
int search(int A[], int n, int target) {
return search(A, , n-, target);
}
int search(int A[], int left, int right, int target)
{
if(left > right)
return -; if(A[left] < A[right])
// one part, binary search
return binarySearch(A, left, right, target); // else, two part
int mid = left + (right-left) / ; //prevent overflow
if(A[mid] == target)
return mid;
else if(A[left] > A[mid])
{// mid is in second part
if(target > A[mid])
{// target may be in the first part (case1), or second part after mid(case2)
if(target == A[left])
return left;
else if(target > A[left])
{// case1
return search(A, left, mid-, target);
}
else
{// case2
return search(A, mid+, right, target);
}
}
else
{// target is in the second part before mid
return search(A, left, mid-, target);
}
}
else
{// mid is in first part
if(target > A[mid])
{// target is in first part after mid
return search(A, mid+, right, target);
}
else
{// target may be in the first part before mid (case1), or second part
if(target == A[left])
return left;
else if(target > A[left])
{// case1
return search(A, left, mid-, target);
}
else
{// case2
return search(A, mid+, right, target);
}
}
}
}
int binarySearch(int A[], int left, int right, int target)
{
while(left <= right)
{
int mid = left + (right-left) / ; //prevent overflow
if(A[mid] == target)
return mid;
else if(A[mid] > target)
right = mid - ;
else
left = mid + ;
}
return -;
}
};

【LeetCode】33. Search in Rotated Sorted Array (4 solutions)的更多相关文章

  1. 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【Leetcode】33. Search in Rotated Sorted Array

    Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...

  3. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  4. 【Leetcode】81. Search in Rotated Sorted Array II

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

  5. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

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

  6. 【LeetCode】081. Search in Rotated Sorted Array II

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

  7. 【一天一道LeetCode】#33. Search in Rotated Sorted Array

    一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...

  8. 【LeetCode】033. Search in Rotated Sorted Array

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  9. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

随机推荐

  1. [转]web服务器apache架构与原理 &apache 监控

    web服务器                                                                                在开始了解Apache前,我 ...

  2. c++多行字符串,可以这么写

    c++多行字符串,可以这么写:CString s;s.Format("CREATE TABLE %s(\[ID] [int] IDENTITY(1,1) NOT NULL,\[Vendor] ...

  3. Appium+python自动化54-appium-doctor报错已解决(SyntaxError: Unexpected token ...)

    前言 由于新版的appium desktop版本是不带appium-doctor这个包的,所以想用appium-desktop检查环境的话需要另外的安装了,在安装的时候小编又遇到了一个坑 报错信息:S ...

  4. Eclipse中设置中文件javadoc

    Eclipse中设置中文件javadoc 在Eclipse中,我们常常看一些英文的JavaDoc提示或者没有相应的提示是很不习惯的,如下图所示: 我们现在要把这种不习惯的提示改为中文的JavaDOC提 ...

  5. 排查sqoop报错:Error running child : java.lang.OutOfMemoryError: Java heap space

    报错栈: -- ::, INFO [main] org.apache.hadoop.mapred.MapTask: Processing split: = AND = -- ::, INFO [mai ...

  6. easyui select 下拉框的取值和赋值

    1.取值 //拍卖管理中示例 function serializeForm(form) { var obj = { auclotType : $('#auclotType').val(), goods ...

  7. 推荐两份学习 Kotlin 和机器学习的资料

    最近 Kotlin 和人工智能比较火,有不少同学留言问我怎么学习 Kotlin,怎么学习机器学习,今天就给大家推荐两份不错的学习资料. 1. Kotlin 学习资料其实,在我看来最好的学习资料就是 K ...

  8. @使用javap反编译Java字节码文件

    在Sun公司提供的JDK中,就已经内置了Java字节码文件反编译工具javap.exe(位于JDK安装目录的bin文件夹下). 我们可以在dos窗口中使用javap来反汇编指定的Java字节码文件.在 ...

  9. JAVA Selenium PHONCOMJS 获取js动态生成完整网页

    首先需要在maven的pom.xml中包含以下依赖 <dependency> <groupId>com.github.detro</groupId> <art ...

  10. Oracle sql loader 使用案例

    Listing 1: ---------------------- dir *.csv type abc.csv sqlplus scott/tiger@orcl create table emp1 ...