Description
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.
The original problem is here.
The original code is here.
My Solution
I solve this problem in C++, as below:
/*
*Search in Rotated Sorted Array
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
*/
#include<iostream>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
class Solution {
public:
int search(vector<int>& nums, int target) {
int size = nums.size();
map<int,int> myMap;
if(size <= 0)
return -1;
for(int i=0;i<size;i++){
myMap[nums[i]] = i;
}
sort(nums.begin(),nums.end());
int start, end, middle;
start = 0;
end = size-1;
while(start <= end){
middle = (start+end) / 2;
if(nums[middle] == target){
return myMap[nums[middle]];
}
else if(nums[middle] > target)
end = middle - 1;
else
start = middle + 1;
}
return -1;
}
};
Note
To solve the problem, binary search is used. I use a map to save the indexes of the numbers.