-
Notifications
You must be signed in to change notification settings - Fork 1
/
search-in-rotated-sorted-array.js
52 lines (41 loc) · 1.3 KB
/
search-in-rotated-sorted-array.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
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
/*
https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/273622/Javascript-Simple-O(log-N)-Binary-Search-Solution
另一种解法,rotated数组,分成一半,一定有一半是有序的,那么不断的找有序的这一半,然后再找target
*/
var search = function(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] === target) {
return mid;
}
// When dividing the roated array into two halves, one must be sorted.
// Check if the left side is sorted
if (nums[left] <= nums[mid]) {
if (nums[left] <= target && target <= nums[mid]) {
// target is in the left
right = mid - 1;
} else {
// target is in the right
left = mid + 1;
}
}
// Otherwise, the right side is sorted
else {
if (nums[mid] <= target && target <= nums[right]) {
// target is in the right
left = mid + 1;
} else {
// target is in the left
right = mid - 1;
}
}
}
return -1;
};