Search in Rotated Sorted Array II by LeetCode
java编写
public boolean search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while(left <= right) {
int mid = (left + right) / 2;
if(target == nums[mid]) return true;
if(nums[mid] < nums[right]) {
if(target < nums[mid] || target > nums[right]) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
else if(nums[mid] > nums[right]) {
if(target < nums[left] || target > nums[mid]) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
else {
while(left <= right && nums[left] == nums[mid]) left++;
while(left <= right && nums[right] == nums[mid]) right–;
}
}
return false;
}
这题是写过类似的,但是还是没写出来。是真的煞笔。啊啊啊啊啊。
Search in Rotated Sorted Array II(Medium)
最新推荐文章于 2021-04-08 13:12:47 发布