Skip to content

Commit 1d82f14

Browse files
update 81
1 parent 418f1ab commit 1d82f14

File tree

1 file changed

+42
-20
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+42
-20
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_81.java

+42-20
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,66 @@
11
package com.fishercoder.solutions;
22

3+
/**
4+
* 81. Search in Rotated Sorted Array II
5+
* <p>
6+
* There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).
7+
* Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is
8+
* [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4].
9+
* Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums.
10+
* You must decrease the overall operation steps as much as possible.
11+
* <p>
12+
* Example 1:
13+
* Input: nums = [2,5,6,0,0,1,2], target = 0
14+
* Output: true
15+
* <p>
16+
* Example 2:
17+
* Input: nums = [2,5,6,0,0,1,2], target = 3
18+
* Output: false
19+
* <p>
20+
* Constraints:
21+
* 1 <= nums.length <= 5000
22+
* -104 <= nums[i] <= 104
23+
* nums is guaranteed to be rotated at some pivot.
24+
* -104 <= target <= 104
25+
* Follow up: This problem is similar to Search in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why?
26+
*/
327
public class _81 {
4-
528
public static class Solution1 {
629
public boolean search(int[] nums, int target) {
7-
int start = 0;
8-
int end = nums.length - 1;
30+
int left = 0;
31+
int right = nums.length - 1;
932

10-
//check each num so we will check start == end
33+
//check each num so we will check left == right
1134
//We always get a sorted part and a half part
1235
//we can check sorted part to decide where to go next
13-
while (start <= end) {
14-
int mid = start + (end - start) / 2;
36+
while (left <= right) {
37+
int mid = left + (right - left) / 2;
1538
if (nums[mid] == target) {
1639
return true;
1740
}
1841

19-
//if left part is sorted
20-
if (nums[start] < nums[mid]) {
21-
if (target < nums[start] || target > nums[mid]) {
42+
if (nums[left] < nums[mid]) {
43+
//if left part is sorted
44+
if (target < nums[left] || target > nums[mid]) {
2245
//target is in rotated part
23-
start = mid + 1;
46+
left = mid + 1;
2447
} else {
25-
end = mid - 1;
48+
right = mid - 1;
2649
}
27-
} else if (nums[start] > nums[mid]) {
28-
//right part is rotated
29-
30-
//target is in rotated part
31-
if (target < nums[mid] || target > nums[end]) {
32-
end = mid - 1;
50+
} else if (nums[left] > nums[mid]) {
51+
//right part is sorted
52+
if (target < nums[mid] || target > nums[right]) {
53+
//target is in rotated part
54+
right = mid - 1;
3355
} else {
34-
start = mid + 1;
56+
left = mid + 1;
3557
}
3658
} else {
37-
//duplicates, we know nums[mid] != target, so nums[start] != target
59+
//duplicates, we know nums[mid] != target, so nums[left] != target
3860
//based on current information, we can only move left pointer to skip one cell
3961
//thus in the worst case, we would have target: 2, and array like 11111111, then
4062
//the running time would be O(n)
41-
start++;
63+
left++;
4264
}
4365
}
4466
return false;

0 commit comments

Comments
 (0)