# 33.Search in Rotated Sorted Array

## August 19, 2016

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

1. 若A[mid]<A[high]，则右半边有序，若target存在于此区间,则将low调整到mid+1处;否则，将high调整到mid-1
2. 若A[mid]>=A[high]，则左半边有序，若target存在于此区间，则将high调到mid-1处;否则，将low调整到mid+1处
3. 若A[mid]==target，说明找到，返回

class Solution {
public:
int search(vector<int>& nums, int target) {
int size=nums.size(),low=0,high=size-1,mid=0;
while(low<=high){
mid=low+(high-low)/2;
if(target==nums[mid])
return mid;
if(nums[mid]<nums[high]){
if(target>nums[mid]&&target<=nums[high])
low=mid+1;
else
high=mid-1;
}
else{
if(target>=nums[low]&&target<nums[mid])
high=mid-1;
else
low=mid+1;
}
}
return -1;
}
};


### 学籍管理系统文档

#### 北邮教务系统评教脚本

Published on September 17, 2017

#### 72.Edit distance

Published on September 17, 2017