Skip to content

Commit 5c11a69

Browse files
committed
81. 搜索旋转排序数组 II
1 parent bf0ddd9 commit 5c11a69

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.gatsby;
2+
3+
/**
4+
* @ClassName: _81SearchInRotatedSortedArrayII
5+
* @Description:
6+
* @author: Gatsby
7+
* @date: 2022/7/25 14:26
8+
*/
9+
10+
public class _81SearchInRotatedSortedArrayII {
11+
public boolean search(int[] nums, int target) {
12+
int left = 0;
13+
int right = nums.length;
14+
while (left < right) {
15+
int mid = (right - left) / 2;
16+
if (nums[mid] == target)
17+
return true;
18+
else if (nums[left] == nums[mid]) {
19+
++left;
20+
} else if (nums[left] < nums[mid]) {
21+
if (nums[left] <= target && target < nums[mid]) right = mid - 1;
22+
else left = mid + 1;
23+
} else {
24+
if (nums[mid] < target && target <= nums[right]) left = mid + 1;
25+
else right = mid - 1;
26+
}
27+
}
28+
return false;
29+
}
30+
}
31+
32+

0 commit comments

Comments
 (0)