Skip to content

Commit 9424726

Browse files
committedJan 1, 2017
Search in a Rotated Sorted Array
1 parent 1450d6b commit 9424726

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Your ideas/fixes/algorithms are more than welcome!
213213
|39|[Combination Sum](https://leetcode.com/problems/combination-sum/)|[Solution](../../blob/master/src/stevesun/algorithms/CombinationSum.java)|O(k*n^k)|O(k)|Medium|Backtracking
214214
|38|[Count and Say](https://leetcode.com/problems/count-and-say/)|[Solution](../../blob/master/src/stevesun/algorithms/CountandSay.java)|O(n*2^n)|O(2^n)|Easy| Recursion, LinkedList
215215
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[Solution](../../blob/master/src/stevesun/algorithms/SearchForARange.java)|O(logn)|O(1)|Medium|Array, Binary Search
216+
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|[Solution](../../blob/master/src/stevesun/algorithms/SearchinRotatedSortedArray.java)|O(logn)|O(1)|Hard|Binary Search
216217
|32|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)|[Solution](../../blob/master/src/stevesun/algorithms/LongestValidParentheses.java)|O(n)|O(n)|Hard|Stack, DP
217218
|31|[Next Permutation](https://leetcode.com/problems/next-permutation)|[Solution](../../blob/master/src/stevesun/algorithms/NextPermutation.java)|O(n)|O(1)|Medium|Array
218219
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/)|[Solution](../../blob/master/src/stevesun/algorithms/ImplementStrStr.java)|O(n)|O(1)|Easy| String
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package stevesun.algorithms;
2+
3+
/**
4+
* Suppose a sorted array is rotated at some pivot unknown to you beforehand.
5+
6+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
7+
8+
You are given a target value to search. If found in the array return its index, otherwise return -1.
9+
10+
You may assume no duplicate exists in the array.
11+
*/
12+
public class SearchinRotatedSortedArray {
13+
14+
public int search(int[] A, int target) {
15+
int len = A.length;
16+
if (len == 0)
17+
return -1;
18+
if (len == 1) {
19+
if (A[0] == target) {
20+
return 0;
21+
} else {
22+
return -1;
23+
}
24+
}
25+
int watershed = A[0];
26+
int watershedIndex = 0;
27+
for (int i = 0; i < len - 1; i++) {
28+
if (A[i] > A[i + 1]) {
29+
watershed = A[i];
30+
watershedIndex = i;
31+
break;
32+
}
33+
}
34+
System.out.println("watershed = " + watershed + "\twatershedIndex = "
35+
+ watershedIndex);
36+
37+
if (target == watershed)
38+
return watershedIndex;
39+
else if (target > watershed) {
40+
/*
41+
* here is the tricky part: when target is greater than watershed,
42+
* it's also possible that this list is ZERO rotated, i.e. it didn't
43+
* rotate at all! Then at this moment, watershed is not the largest
44+
* element int this array, so we need to binary search this whole
45+
* array.
46+
*/
47+
if (watershedIndex == 0) {
48+
int start = 0;
49+
int end = len - 1;
50+
int mid = (start + end) / 2;
51+
while (start <= end) {
52+
if (target > A[mid]) {
53+
start = mid + 1;
54+
mid = (start + end) / 2;
55+
} else if (target < A[mid]) {
56+
end = mid - 1;
57+
mid = (start + end) / 2;
58+
} else if (target == A[mid]) {
59+
return mid;
60+
}
61+
}
62+
return -1;
63+
} else
64+
return -1;
65+
} else if (target < watershed) {
66+
/*
67+
* target could be in either part of this sorted array, then we
68+
* check if target is greater than A[0], if so, then search in the
69+
* first part, if not, then check if it is greater than A[len - 1],
70+
* if so, return -1, if not, search in the second part
71+
*/
72+
73+
if (target == A[0]) {
74+
return 0;
75+
} else if (target > A[0]) {
76+
int start = 1;
77+
int end = watershedIndex - 1;
78+
int mid = (start + end) / 2;
79+
while (start <= end) {
80+
if (target > A[mid]) {
81+
start = mid + 1;
82+
mid = (start + end) / 2;
83+
} else if (target < A[mid]) {
84+
end = mid - 1;
85+
mid = (start + end) / 2;
86+
} else if (target == A[mid]) {
87+
return mid;
88+
}
89+
}
90+
return -1;
91+
} else if (target < A[0]) {
92+
if (target == A[len - 1]) {
93+
return len - 1;
94+
} else if (target > A[len - 1]) {
95+
return -1;
96+
} else if (target < A[len - 1]) {
97+
int start = watershedIndex + 1;
98+
int end = len - 2;
99+
int mid = (start + end) / 2;
100+
while (start <= end) {
101+
if (target > A[mid]) {
102+
start = mid + 1;
103+
mid = (start + end) / 2;
104+
} else if (target < A[mid]) {
105+
end = mid - 1;
106+
mid = (start + end) / 2;
107+
} else if (target == A[mid]) {
108+
return mid;
109+
}
110+
}
111+
return -1;
112+
}
113+
}
114+
}
115+
return -1;
116+
}
117+
118+
}

0 commit comments

Comments
 (0)
Please sign in to comment.