您的位置:首页 > 其它

leetcode:33. Search in Rotated Sorted Array

2017-03-15 14:01 393 查看

描述

Suppose an array sorted in ascending order 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.

思路

采用二分查找,这里middle位置的数不仅要跟target比较,而且还要与nums中最开始的数比较,这样才能决定往左还是往右走。下面的代码是借鉴了别人的哦。

代码

class Solution {
public:
int search(vector<int>& nums, int target) {
int low = 0, high = nums.size();
int mid;
double num;
while (low < high) {
mid = (low + high) / 2;

num = (nums[mid] < nums[0]) == (target < nums[0])
? nums[mid]
: target < nums[0] ? -INFINITY : INFINITY;

if (num < target)
low = mid + 1;
else if (num > target)
high = mid;
else
return mid;
}
return -1;
}
};


结果



他山之玉

C/C++ O(n) solutioin

class Solution {
public:
int search(int A[], int n, int target) {
int lo=0,hi=n-1;
// find the index of the smallest value using binary search.
// Loop will terminate since mid < hi, and lo or hi will shrink by at least 1.
// Proof by contradiction that mid < hi: if mid==hi, then lo==hi and loop would have been terminated.
while(lo<hi){
int mid=(lo+hi)/2;
if(A[mid]>A[hi]) lo=mid+1;
else hi=mid;
}
// lo==hi is the index of the smallest value and also the number of places rotated.
int rot=lo;
lo=0;hi=n-1;
// The usual binary search and accounting for rotation.
while(lo<=hi){
int mid=(lo+hi)/2;
int realmid=(mid+rot)%n;
if(A[realmid]==target)return realmid;
if(A[realmid]<target)lo=mid+1;
else hi=mid-1;
}
return -1;
}
};


Java O(n) solution

public class Solution {
// assume no duplicate exists
public int search(int[] nums, int target) {
if(nums==null || nums.length==0) return -1;
int res = -1;
int peak = findPeak(nums);
if(target == nums[peak]) return peak;
if(target>nums[0]){
res = Arrays.binarySearch(nums, 0, peak, target);
} else if(target<nums[0]){
res = res = Arrays.binarySearch(nums, peak+1, nums.length, target);
} else  return 0;

return res<0 ? -1 : res;
}

private int findPeak(int[] nums){
int left = 0, right = nums.length-1;
while(left<=right){
if(isPeak(nums, left)) return left;
if(isPeak(nums, right)) return right;
int mid = left+(right-left)/2;
if(isPeak(nums, mid)) return mid;
if(nums[mid]>nums[right]){
left = mid+1;
} else { //if(nums[mid]<nums[right]) // no duplicate
right = mid-1;
}
}
return left;
}

private boolean isPeak(int[] nums, int index){
int right = (index+1) % nums.length;
return nums[index]>nums[right];
}
}


python O(n) solution

class Solution(object):
def find_pivot(self, nums):
low,high = 0, len(nums)-1
while low < high:
mid = low + (high-low)//2
if nums[mid] > nums[high]:
low = mid + 1 # Note that mid+1 is important.
else:
high = mid
return low

def bsearch(self, nums, low, high, target):
while low <= high:
mid = low + int((high-low)/2)
if nums[mid] == target:
return mid
elif nums[mid] > target:
high = mid - 1
else:
low = mid + 1
return -1

def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
high = len(nums)-1
pivot = self.find_pivot(nums)
if target >= nums[pivot] and target <= nums[high]:
return self.bsearch(nums, pivot, high, target)
else:
return self.bsearch(nums, 0, pivot-1, target)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 搜索