您的位置:首页 > 其它

81. Search in Rotated Sorted Array II

2016-03-21 11:10 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.

Subscribe to see which companies asked this question

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