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;
}
}
本文介绍了一种在允许重复元素的旋转有序数组中查找特定目标值的方法。文章提供了一个Java实现示例,详细展示了如何通过调整二分查找算法来解决这一问题,并特别注意了重复元素对算法效率的影响。
686

被折叠的 条评论
为什么被折叠?



