您的位置:首页 > 其它

LeetCode *** 81. Search in Rotated Sorted Array II

2016-04-15 10:43 267 查看
题目:

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(vector<int>& nums, int target) {
if(nums.size()<1)return false;

int low=0,high=nums.size()-1,mid;

while(low<high){
mid=(low+high)/2;
if(nums[mid]==target)return true;

if(nums[mid]>nums[high]){
if(nums[mid]>target&&nums[low]<=target)high=mid;
else low=mid+1;
}
else if(nums[mid]<nums[high]){
if(nums[mid]<target&&nums[high]>=target)low=mid+1;
else high=mid;
}else high--;

}
return nums[low]==target;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: