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

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.

This is a classic interview question. It's solution is similar with normal binary search. The only difference is that we need to add more conditional sentences.

The key points is when we divide the array into two half, we need to compare A[mid] with one of the end element of the array, so that we can know which half of the array is sorted, and which half is rotated, and divided them again subsequently.

The difference I do here is that I add one normal binary search here as  a helper function. Actually you don't need the binary search function, just one function will be alright. But that's fun to add them together to check the difference between them.

class Solution {
public:
int search(int A[], int n, int target)
{
return unordBiSearch(A, 0, n-1, target);
}

int unordBiSearch(int A[], int low, int up, int tar)
{
if (low > up) return -1;

int mid = (low+up)>>1;
if (A[mid] == tar)
return mid;
if (A[mid]>A[up])
{
if (A[low] <= tar && A[mid] > tar)
return biSearch(A, low, mid-1, tar);
else return unordBiSearch(A, mid+1, up, tar);
}
if (A[mid]<A[up])
{
if (A[mid] < tar && A[up] >= tar)
return biSearch(A, mid+1, up, tar);
else return unordBiSearch(A, low, mid-1, tar);
}
return -1;
}

int biSearch(int A[], int low, int up, int key)
{
if(low>up) return -1;
int mid = (low+up)>>1;
if (key < A[mid])
return biSearch(A, low, mid-1, key);
else if (A[mid] < key)
return biSearch(A, mid+1, up, key);
return mid;
}
};

//2014-1-26 update
int search(int A[], int n, int target)
{
return biSearch(A, 0, n-1, target);
}

int biSearch(int A[], int low, int up, int tar)
{
if (low > up) return -1;
int mid = low + ((up-low)>>1);
if (tar == A[mid]) return mid;

if (A[low] <= A[mid])
{
//容易错漏了A[low]<=tar的条件,容易遗漏等号
if (A[low] <= tar && tar < A[mid]) return biSearch(A, low, mid-1, tar);
else return biSearch(A, mid+1, up, tar);
}
else
{
//容易错漏
if (A[mid] < tar && tar <= A[up]) return biSearch(A, mid+1, up, tar);
else return biSearch(A, low, mid-1, tar);
}
return -1;
}

#### Search in Rotated Sorted Array II -- LeetCode

2014-03-06 02:04:55

#### 【LeetCode】Search in Rotated Sorted Array 解题报告

2014-10-25 17:00:36

#### LeetCode 33 — Search in Rotated Sorted Array（C++ Java Python）

2014-04-03 14:20:40

#### 【LEETCODE】33-Search in Rotated Sorted Array [Python]

2016-01-22 11:26:41

#### [leetcode] 33. Search in Rotated Sorted Array 解题报告

2016-01-03 14:14:24

#### LeetCode 33 Search in Rotated Sorted Array（在旋转排序数组中搜索）（*）

2015-11-26 18:50:31

#### LeetCode --- 81. Search in Rotated Sorted Array II

2015-04-03 17:03:46

#### Search in Rotated Sorted Array -- LeetCode

2014-03-05 09:15:45

#### [LeetCode]33.Search in Rotated Sorted Array

2014-01-15 17:18:33

#### [LeetCode]81.Search in Rotated Sorted Array II

2014-01-15 22:29:05

## 不良信息举报

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