您的位置:首页 > 其它

leetcode---search-in-rotated-sorted-array-ii---查找

2018-03-03 16:27 363 查看
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 l = 0;
int r = n-1;

while(l <= r)
{
int m = (l + r) / 2;

if(A[m] == target)
return true;

if(A[l] < A[m])
{
if(A[m] > target && A[l] <= target)
r = m-1;
else
l = m+1;
}
else if(A[m] < A[l])
{
if(A[m] < target && A[r] >= target)
l = m+1;
else
r = m-1;
}
else
++l;
}
return false;
}
};


参考

http://blog.csdn.net/linhuanmars/article/details/20588511
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: