-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
47 lines (39 loc) · 858 Bytes
/
solution.ts
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 id=33 lang=javascript
*
* [33] Search in Rotated Sorted Array
*/
// @lc code=start
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
const search = (nums: number[], target: number): number => {
// * ['52 ms', '88.13 %', '33 MB', '100 %']
let l = 0;
let r = nums.length - 1;
while (l <= r) {
const mid = ~~((l + r) / 2);
const valLeft = nums[l];
const valRight = nums[r];
const valMid = nums[mid];
if (valMid === target) return mid;
if (valMid < valRight) {
if (valMid < target && target <= valRight) {
l = mid + 1;
} else {
r = mid - 1;
}
} else {
if (valLeft <= target && target < valMid) {
r = mid - 1;
} else {
l = mid + 1;
}
}
}
return -1;
};
// @lc code=end
export { search };