Skip to content

Commit 891b6c0

Browse files
committed
Search In Rotated Sorted Array
1 parent 720398a commit 891b6c0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// TC: O(log n)
2+
// -> binary search
3+
// SC: O(1)
4+
class Solution {
5+
public int search(int[] nums, int target) {
6+
7+
int start = 0;
8+
int end = nums.length - 1;
9+
10+
while (start <= end) {
11+
int mid = start + (end - start) / 2;
12+
13+
if (nums[mid] == target) return mid;
14+
15+
if (nums[start] <= nums[mid]) {
16+
if (nums[start] <= target && target < nums[mid]) end = mid - 1;
17+
else start = mid + 1;
18+
} else {
19+
if (nums[mid] < target && target <= nums[end]) start = mid + 1;
20+
else end = mid - 1;
21+
}
22+
}
23+
return -1;
24+
}
25+
}

0 commit comments

Comments
 (0)