您的位置:首页 > 其它

[leetcode 81] Search in Rotated Sorted Array II

2015-01-01 23:35 381 查看
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.

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