Skip to content

Commit 796b845

Browse files
committed
Time: 0 ms (100.00%), Space: 41.7 MB (89.54%) - LeetHub
1 parent ffbf62c commit 796b845

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// search the number in the rotated sorted array; solve this in n(logn) time
2+
3+
// method: binary search-left&right pointer
4+
5+
class Solution {
6+
public int search(int[] nums, int target) {
7+
int start = 0, end = nums.length - 1;
8+
while (start <= end) {
9+
int mid = start + (end - start) / 2;
10+
if (nums[mid] == target) return mid;
11+
else if (nums[mid] >= nums[start]) {
12+
if (target >= nums[start] && target < nums[mid]) end = mid - 1;
13+
else start = mid + 1;
14+
}
15+
else {
16+
if (target <= nums[end] && target > nums[mid]) start = mid + 1;
17+
else end = mid - 1;
18+
}
19+
}
20+
return -1;
21+
}
22+
}

0 commit comments

Comments
 (0)