Skip to content

Commit f6c7f36

Browse files
refactor 81
1 parent ee43c94 commit f6c7f36

File tree

1 file changed

+37
-59
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+37
-59
lines changed
Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,47 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 81. Search in Rotated Sorted Array II
5-
*
6-
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
7-
*
8-
* (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
9-
*
10-
* You are given a target value to search. If found in the array return true, otherwise return false.
11-
*
12-
* Example 1:
13-
*
14-
* Input: nums = [2,5,6,0,0,1,2], target = 0
15-
* Output: true
16-
* Example 2:
17-
*
18-
* Input: nums = [2,5,6,0,0,1,2], target = 3
19-
* Output: false
20-
* Follow up:
21-
*
22-
* This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
23-
* Would this affect the run-time complexity? How and why?
24-
*/
253
public class _81 {
264

27-
public static class Solution1 {
28-
public boolean search(int[] nums, int target) {
29-
int start = 0;
30-
int end = nums.length - 1;
5+
public static class Solution1 {
6+
public boolean search(int[] nums, int target) {
7+
int start = 0;
8+
int end = nums.length - 1;
319

32-
//check each num so we will check start == end
33-
//We always get a sorted part and a half part
34-
//we can check sorted part to decide where to go next
35-
while (start <= end) {
36-
int mid = start + (end - start) / 2;
37-
if (nums[mid] == target) {
38-
return true;
39-
}
10+
//check each num so we will check start == end
11+
//We always get a sorted part and a half part
12+
//we can check sorted part to decide where to go next
13+
while (start <= end) {
14+
int mid = start + (end - start) / 2;
15+
if (nums[mid] == target) {
16+
return true;
17+
}
4018

41-
//if left part is sorted
42-
if (nums[start] < nums[mid]) {
43-
if (target < nums[start] || target > nums[mid]) {
44-
//target is in rotated part
45-
start = mid + 1;
46-
} else {
47-
end = mid - 1;
48-
}
49-
} else if (nums[start] > nums[mid]) {
50-
//right part is rotated
19+
//if left part is sorted
20+
if (nums[start] < nums[mid]) {
21+
if (target < nums[start] || target > nums[mid]) {
22+
//target is in rotated part
23+
start = mid + 1;
24+
} else {
25+
end = mid - 1;
26+
}
27+
} else if (nums[start] > nums[mid]) {
28+
//right part is rotated
5129

52-
//target is in rotated part
53-
if (target < nums[mid] || target > nums[end]) {
54-
end = mid - 1;
55-
} else {
56-
start = mid + 1;
57-
}
58-
} else {
59-
//duplicates, we know nums[mid] != target, so nums[start] != target
60-
//based on current information, we can only move left pointer to skip one cell
61-
//thus in the worst case, we would have target: 2, and array like 11111111, then
62-
//the running time would be O(n)
63-
start++;
30+
//target is in rotated part
31+
if (target < nums[mid] || target > nums[end]) {
32+
end = mid - 1;
33+
} else {
34+
start = mid + 1;
35+
}
36+
} else {
37+
//duplicates, we know nums[mid] != target, so nums[start] != target
38+
//based on current information, we can only move left pointer to skip one cell
39+
//thus in the worst case, we would have target: 2, and array like 11111111, then
40+
//the running time would be O(n)
41+
start++;
42+
}
43+
}
44+
return false;
6445
}
65-
}
66-
return false;
6746
}
68-
}
6947
}

0 commit comments

Comments
 (0)