Skip to content

Commit 2527328

Browse files
committed
🔥 Search in Rotated Sorted Array
1 parent 8752ac2 commit 2527328

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Solutions of LeetCode Blind 75 Problems in JavaScript
1313
| [Maximum Subarray](./maximum-subarray.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Dynamic Programming`, `Divide & Conquer` | [:link:](https://leetcode.com/problems/maximum-subarray/) |
1414
| [ Maximum Product Subarray](./maximum-product-subarray.js) | <img src="https://img.shields.io/badge/-Medium-orange" /> | `Array`, `Dynamic Programming` | [:link:](https://leetcode.com/problems/maximum-product-subarray/) |
1515
| [Find Minimum in Rotated Sorted Array](./find-minimum-in-rotated-sorted-array.js) | <img src="https://img.shields.io/badge/-Medium-orange" /> | `Array`, `Binary Search` | [:link:](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) |
16+
| [Search in Rotated Sorted Array](./search-in-rotated-sorted-array.js) | <img src="https://img.shields.io/badge/-Medium-orange" /> | `Array`, `Binary Search` | [:link:](https://leetcode.com/problems/search-in-rotated-sorted-array/) |

search-in-rotated-sorted-array.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const search = (nums, target) => {
2+
let result = -1,
3+
low = 0,
4+
high = nums.length - 1,
5+
mid;
6+
7+
while (low <= high) {
8+
mid = Math.floor((low + high) / 2);
9+
10+
if (nums[mid] === target) {
11+
result = mid;
12+
break;
13+
}
14+
15+
if (nums[low] <= nums[mid]) {
16+
if (target > nums[mid] || target < nums[low]) {
17+
low = mid + 1;
18+
} else {
19+
high = mid - 1;
20+
}
21+
} else {
22+
if (target < nums[mid] || target > nums[high]) {
23+
high = mid - 1;
24+
} else {
25+
low = mid + 1;
26+
}
27+
}
28+
}
29+
30+
return result;
31+
};

0 commit comments

Comments
 (0)