您的位置:首页 > 其它

LeetCode——Search in Rotated Sorted Array II

2014-08-09 10:49 337 查看
Follow up for "Search in Rotated Sorted Array":

What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.
原题链接:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/
题目:继续之前的题,在旋转过的有序数组中找目标值。若允许有重复的值呢?

public boolean search(int[] A, int target) {
int low = 0, high = A.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (target == A[mid])
return true;
if (A[mid] > A[low]) {
if (target >= A[low] && target <= A[mid])
high = mid - 1;
else
low = mid + 1;
} else if (A[mid] < A[low]) {
if (target >= A[mid] && target <= A[high])
low = mid + 1;
else
high = mid - 1;
} else
low += 1;
}
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode