From 81d4ae749bb802eb607732b2561fa52c71422f8a Mon Sep 17 00:00:00 2001 From: LouisFonda Date: Sun, 22 Jun 2025 00:26:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=9F=A5=E6=89=BE33,34,35,74?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binary-search/33搜索旋转排序数组.js | 46 +++++++++++++ .../34在排序数组中查找元素出现的范围.js | 40 +++++++++++ .../binary-search/35返回插入的位置.js | 32 +++++++++ .../binary-search/74搜索二维矩阵.js | 67 +++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 top-interview-leetcode150/binary-search/33搜索旋转排序数组.js create mode 100644 top-interview-leetcode150/binary-search/34在排序数组中查找元素出现的范围.js create mode 100644 top-interview-leetcode150/binary-search/35返回插入的位置.js create mode 100644 top-interview-leetcode150/binary-search/74搜索二维矩阵.js diff --git a/top-interview-leetcode150/binary-search/33搜索旋转排序数组.js b/top-interview-leetcode150/binary-search/33搜索旋转排序数组.js new file mode 100644 index 0000000..a963a3b --- /dev/null +++ b/top-interview-leetcode150/binary-search/33搜索旋转排序数组.js @@ -0,0 +1,46 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +const search = function (nums, target) { + +}; + +/* +思路:按照题目描述,数组旋转之后会变成两段有序的序列,而且第一段有序序列一定是大于第二段有序序列的 +可以直接利用二分查找,但是在查找的时候要留意mid落在那一段序列上,如果落在第一段序列上 nums[mid] >= nums[left], +反之第二段,知道mid落在那一段序列之后,我们还需判断target在那一段序列上,如果mid在第一段,target也在第一段并且 +小于nums[mid]收缩右边界为mid-1,如果在第二段,收缩左边界为mid+1 +*/ + +function f1(nums, target) { + let left = 0; + let right = nums.length - 1; + + while (left <= right) { + const mid = left + ((right - left) >> 1); + if (nums[mid] === target) { + return mid; // 找到目标值,直接返回 + } + + // 如果mid落在第一段,那么nums[mid] >= nums[0], 否则落在第二段 + if (nums[mid] >= nums[0]) { + // 如果target在第一段,并且target < nums[mid] + if (target > -nums[0] && target < nums[mid]) { + right = mid - 1; + } else { + left = mid + 1; + } + } else { + // 如果target落在第二段,并且target > nums[mid] + // eslint-disable-next-line no-lonely-if + if (target <= nums[nums.length - 1] && target > nums[mid]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + } + return -1; // 没有找到目标值 +} diff --git a/top-interview-leetcode150/binary-search/34在排序数组中查找元素出现的范围.js b/top-interview-leetcode150/binary-search/34在排序数组中查找元素出现的范围.js new file mode 100644 index 0000000..1009126 --- /dev/null +++ b/top-interview-leetcode150/binary-search/34在排序数组中查找元素出现的范围.js @@ -0,0 +1,40 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +const searchRange = function (nums, target) { + +}; + + +function searchRange(nums, target) { + let left = 0, right = nums.length - 1; + let start = -1, end = -1; + + // Find the first occurrence + while (left <= right) { + let mid = Math.floor((left + right) / 2); + if (nums[mid] >= target) { + right = mid - 1; + } else { + left = mid + 1; + } + } + start = nums[left] === target ? left : -1; + + // Find the last occurrence + left = 0; + right = nums.length - 1; + while (left <= right) { + let mid = Math.floor((left + right) / 2); + if (nums[mid] <= target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + end = nums[right] === target ? right : -1; + + return [start, end]; +} diff --git a/top-interview-leetcode150/binary-search/35返回插入的位置.js b/top-interview-leetcode150/binary-search/35返回插入的位置.js new file mode 100644 index 0000000..68c8488 --- /dev/null +++ b/top-interview-leetcode150/binary-search/35返回插入的位置.js @@ -0,0 +1,32 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +const searchInsert = function (nums, target) { + +}; + +/* +直接利用二分查找 +*/ + let pos = nums.length; + let left = 0; + let right = nums.length - 1; + + // 二分查找知道left>right结束查找 + while (left <= right) { + const mid = Math.floor((left + right) / 2); + // 如果target