forked from jyxia/LeetCode-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path33-searchRotatedSortedArray.js
61 lines (54 loc) · 1.8 KB
/
33-searchRotatedSortedArray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Binary search. In the rotated array, there must be a half of the array in sorted order.
* @see the recursive method. If the target is in the sorted half, binary search the target in
* this half. Otherwise, search the target in the other half. And recursively do this process until
* the target is found.
*
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
return searchHelper(nums, 0, nums.length - 1, target);
};
var searchHelper = function(nums, left, right, target) {
if (right < left) return -1;
var mid = left + Math.floor((right - left) / 2);
if (nums[mid] === target) return mid;
if (nums[left] <= nums[mid]) {
if (target >= nums[left] && target < nums[mid]) {
return searchHelper(nums, left, mid - 1, target);
} else {
return searchHelper(nums, mid + 1, right, target);
}
} else {
if (target <= nums[right] && target > nums[mid]) {
return searchHelper(nums, mid + 1, right, target);
} else {
return searchHelper(nums, left, mid - 1, target);
}
}
};
// Interative
var search = function(nums, target) {
var left = 0;
var right = nums.length - 1;
while (left < right) {
var mid = left + Math.floor((right - left) / 2);
if (nums[mid] === target) return mid;
if (nums[left] <= nums[mid]) {
if (target >= nums[left] && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if (target <= nums[right] && target > nums[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return nums[left] === target ? left : -1;
};