|
4 | 4 | * 81. Search in Rotated Sorted Array II
|
5 | 5 | *
|
6 | 6 | * Follow up for "Search in Rotated Sorted Array":
|
| 7 | + * |
7 | 8 | * What if duplicates are allowed?
|
8 | 9 | * Would this affect the run-time complexity? How and why?
|
9 | 10 | * Write a function to determine if a given target is in the array.
|
10 | 11 | */
|
11 | 12 | public class _81 {
|
12 | 13 |
|
13 |
| - public static class Solution1 { |
14 |
| - public boolean search(int[] A, int target) { |
15 |
| - int len = A.length; |
16 |
| - if (len == 0) { |
17 |
| - return false; |
18 |
| - } |
19 |
| - if (len == 1) { |
20 |
| - if (A[0] == target) { |
21 |
| - return true; |
22 |
| - } else { |
23 |
| - return false; |
24 |
| - } |
25 |
| - } |
26 |
| - int watershed = A[0]; |
27 |
| - int watershedIndex = 0; |
28 |
| - for (int i = 0; i < len - 1; i++) { |
29 |
| - if (A[i] > A[i + 1]) { |
30 |
| - watershed = A[i]; |
31 |
| - watershedIndex = i; |
32 |
| - System.out.println("Place 1: watershed = " + watershed |
33 |
| - + "\twatershedIndex = " + watershedIndex); |
34 |
| - for (int j = i + 1; j < len; j++) { |
35 |
| - if (A[j] == A[i]) { |
36 |
| - watershed = A[j]; |
37 |
| - watershedIndex = j; |
38 |
| - System.out.println("Place 2: watershed = " + watershed |
39 |
| - + "\twatershedIndex = " + watershedIndex); |
40 |
| - } else { |
41 |
| - break; |
42 |
| - } |
43 |
| - } |
44 |
| - } |
45 |
| - } |
46 |
| - System.out.println("watershed = " + watershed + "\twatershedIndex = " |
47 |
| - + watershedIndex); |
48 |
| - if (target == watershed) { |
49 |
| - return true; |
50 |
| - } else if (target > watershed) { |
51 |
| - /* |
52 |
| - * here is the tricky part: when target is greater than watershed, |
53 |
| - * it's also possible that this list is ZERO rotated, i.e. it didn't |
54 |
| - * rotate at all! Then at this moment, watershed is not the largest |
55 |
| - * element int this array, so we need to binary search this whole |
56 |
| - * array. |
57 |
| - */ |
58 |
| - if (watershedIndex == 0) { |
59 |
| - int start = 0; |
60 |
| - int end = len - 1; |
61 |
| - int mid = (start + end) / 2; |
62 |
| - while (start <= end) { |
63 |
| - if (target > A[mid]) { |
64 |
| - start = mid + 1; |
65 |
| - mid = (start + end) / 2; |
66 |
| - } else if (target < A[mid]) { |
67 |
| - end = mid - 1; |
68 |
| - mid = (start + end) / 2; |
69 |
| - } else if (target == A[mid]) { |
70 |
| - return true; |
71 |
| - } |
72 |
| - } |
73 |
| - return false; |
74 |
| - } else { |
75 |
| - return false; |
76 |
| - } |
77 |
| - } else if (target < watershed) { |
78 |
| - /* |
79 |
| - * target could be in either part of this sorted array, then we |
80 |
| - * check if target is greater than A[0], if so, then search in the |
81 |
| - * first part, if not, then check if it is greater than A[len - 1], |
82 |
| - * if so, return -1, if not, search in the second part |
83 |
| - */ |
84 |
| - |
85 |
| - if (target == A[0]) { |
86 |
| - return true; |
87 |
| - } else if (target > A[0]) { |
88 |
| - int start = 1; |
89 |
| - int end = watershedIndex - 1; |
90 |
| - int mid = (start + end) / 2; |
91 |
| - while (start <= end) { |
92 |
| - if (target > A[mid]) { |
93 |
| - start = mid + 1; |
94 |
| - mid = (start + end) / 2; |
95 |
| - } else if (target < A[mid]) { |
96 |
| - end = mid - 1; |
97 |
| - mid = (start + end) / 2; |
98 |
| - } else if (target == A[mid]) { |
99 |
| - return true; |
100 |
| - } |
101 |
| - } |
102 |
| - return false; |
103 |
| - } else if (target < A[0]) { |
104 |
| - if (target == A[len - 1]) { |
105 |
| - return true; |
106 |
| - } else if (target > A[len - 1]) { |
107 |
| - return false; |
108 |
| - } else if (target < A[len - 1]) { |
109 |
| - int start = watershedIndex + 1; |
110 |
| - int end = len - 2; |
111 |
| - int mid = (start + end) / 2; |
112 |
| - while (start <= end) { |
113 |
| - if (target > A[mid]) { |
114 |
| - start = mid + 1; |
115 |
| - mid = (start + end) / 2; |
116 |
| - } else if (target < A[mid]) { |
117 |
| - end = mid - 1; |
118 |
| - mid = (start + end) / 2; |
119 |
| - } else if (target == A[mid]) { |
120 |
| - return true; |
121 |
| - } |
122 |
| - } |
123 |
| - return false; |
124 |
| - } |
125 |
| - } |
126 |
| - } |
127 |
| - return false; |
| 14 | + public static class Solution1 { |
| 15 | + public boolean search(int[] nums, int target) { |
| 16 | + int start = 0; |
| 17 | + int end = nums.length - 1; |
| 18 | + |
| 19 | + //check each num so we will check start == end |
| 20 | + //We always get a sorted part and a half part |
| 21 | + //we can check sorted part to decide where to go next |
| 22 | + while (start <= end) { |
| 23 | + int mid = start + (end - start) / 2; |
| 24 | + if (nums[mid] == target) { |
| 25 | + return true; |
128 | 26 | }
|
129 |
| - } |
130 |
| - |
131 |
| - public static class Solution2 { |
132 |
| - public boolean search(int[] nums, int target) { |
133 |
| - int start = 0; |
134 |
| - int end = nums.length - 1; |
135 |
| - |
136 |
| - //check each num so we will check start == end |
137 |
| - //We always get a sorted part and a half part |
138 |
| - //we can check sorted part to decide where to go next |
139 |
| - while (start <= end) { |
140 |
| - int mid = start + (end - start) / 2; |
141 |
| - if (nums[mid] == target) { |
142 |
| - return true; |
143 |
| - } |
144 |
| - |
145 |
| - //if left part is sorted |
146 |
| - if (nums[start] < nums[mid]) { |
147 |
| - if (target < nums[start] || target > nums[mid]) { |
148 |
| - //target is in rotated part |
149 |
| - start = mid + 1; |
150 |
| - } else { |
151 |
| - end = mid - 1; |
152 |
| - } |
153 |
| - } else if (nums[start] > nums[mid]) { |
154 |
| - //right part is rotated |
155 |
| - |
156 |
| - //target is in rotated part |
157 |
| - if (target < nums[mid] || target > nums[end]) { |
158 |
| - end = mid - 1; |
159 |
| - } else { |
160 |
| - start = mid + 1; |
161 |
| - } |
162 |
| - } else { |
163 |
| - //duplicates, we know nums[mid] != target, so nums[start] != target |
164 |
| - //based on current information, we can only move left pointer to skip one cell |
165 |
| - //thus in the worst case, we would have target: 2, and array like 11111111, then |
166 |
| - //the running time would be O(n) |
167 |
| - start++; |
168 |
| - } |
169 |
| - } |
170 | 27 |
|
171 |
| - return false; |
| 28 | + //if left part is sorted |
| 29 | + if (nums[start] < nums[mid]) { |
| 30 | + if (target < nums[start] || target > nums[mid]) { |
| 31 | + //target is in rotated part |
| 32 | + start = mid + 1; |
| 33 | + } else { |
| 34 | + end = mid - 1; |
| 35 | + } |
| 36 | + } else if (nums[start] > nums[mid]) { |
| 37 | + //right part is rotated |
| 38 | + |
| 39 | + //target is in rotated part |
| 40 | + if (target < nums[mid] || target > nums[end]) { |
| 41 | + end = mid - 1; |
| 42 | + } else { |
| 43 | + start = mid + 1; |
| 44 | + } |
| 45 | + } else { |
| 46 | + //duplicates, we know nums[mid] != target, so nums[start] != target |
| 47 | + //based on current information, we can only move left pointer to skip one cell |
| 48 | + //thus in the worst case, we would have target: 2, and array like 11111111, then |
| 49 | + //the running time would be O(n) |
| 50 | + start++; |
172 | 51 | }
|
| 52 | + } |
| 53 | + return false; |
173 | 54 | }
|
| 55 | + } |
174 | 56 | }
|
0 commit comments