package com.zhumq.lianxi;
public class SearchinRotatedSortedArrayII {
// Search in Rotated Sorted Array II
// Time Complexity: O(n),Space Complexity: O(1)
public boolean search(int[] nums, int target) {
int first = 0, last = nums.length;
while (first != last) {
final int mid = first + (last - first) / 2;
if (nums[mid] == target)
return true;
if (nums[first] < nums[mid]) {
if (nums[first] <= target && target < nums[mid])
last = mid;
else
first = mid + 1;
} else if (nums[first] > nums[mid]) {
if (nums[mid] < target && target <= nums[last-1])
first = mid + 1;
else
last = mid;
} else
//只多出来这一步,当最左边值等于中间值时就从最左边往右走一步接着判断
first++;
}
return false;
}
}
LeetCode------Search in Rotated Sorted Array II
最新推荐文章于 2024-05-03 20:14:24 发布