-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path33.搜索旋转排序数组.js
47 lines (46 loc) · 980 Bytes
/
33.搜索旋转排序数组.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
/*
* @lc app=leetcode.cn id=33 lang=javascript
*
* [33] 搜索旋转排序数组
*/
// @lc code=start
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function (nums, target) {
const n = nums.length
if (!n) return -1
let l = 0,
r = n - 1
while (l <= r) {
if (l === r) {
return nums[l] === target ? l : -1
}
// if (nums[l] === target) return l
// if (nums[r] === target) return r
const mid = (l + r) >> 1
if (nums[mid] === target) return mid
if (nums[0] < nums[mid]) {
if (nums[0] < target && target < nums[mid]) {
// l = l + 1
r = mid - 1
} else {
l = mid + 1
// r = r - 1
}
} else {
if (nums[mid] < target && target < nums[n - 1]) {
l = mid + 1
// r = r - 1
} else {
// l = l + 1
r = mid - 1
}
}
}
return -1
}
console.log(search([4, 5, 6, 7, 0, 1, 2], 0))
// @lc code=end