您的位置:首页 > 其它

Leetcode no. 81

2016-04-13 21:50 211 查看
81. Search in Rotated Sorted Array II

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.

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