陣列搜尋是資料結構和演算法(DSA)中的基本概念。這篇部落格文章將介紹使用 JavaScript 的各種陣列搜尋技術,從基礎到進階。我們將探索 20 個範例,討論時間複雜度,並提供 LeetCode 練習題。
線性搜尋是最簡單的搜尋演算法,適用於排序和未排序的陣列。
時間複雜度: O(n),其中 n 是陣列中的元素數量。
function linearSearch(arr, target) { for (let i = 0; i <h3> 範例 2:尋找所有出現的情況 </h3> <pre class="brush:php;toolbar:false">function findAllOccurrences(arr, target) { const result = []; for (let i = 0; i <h2> 2.二分查找 </h2> <p>二分搜尋是一種在排序數組中搜尋的有效演算法。 </p> <p><strong>時間複雜度:</strong> O(log n)</p> <h3> 範例 3:迭代二分搜尋 </h3> <pre class="brush:php;toolbar:false">function binarySearch(arr, target) { let left = 0; let right = arr.length - 1; while (left <h3> 範例 4:遞歸二分查找 </h3> <pre class="brush:php;toolbar:false">function recursiveBinarySearch(arr, target, left = 0, right = arr.length - 1) { if (left > right) { return -1; } const mid = Math.floor((left + right) / 2); if (arr[mid] === target) { return mid; } else if (arr[mid] <h2> 3. 跳轉搜索 </h2> <p>跳躍搜尋是一種排序數組演算法,它透過跳過一些元素來減少比較次數。 </p> <p><strong>時間複雜度:</strong> O(√n)</p> <h3> 範例 5:跳轉搜尋實現 </h3> <pre class="brush:php;toolbar:false">function jumpSearch(arr, target) { const n = arr.length; const step = Math.floor(Math.sqrt(n)); let prev = 0; while (arr[Math.min(step, n) - 1] = n) { return -1; } } while (arr[prev] <h2> 4.插值搜索 </h2> <p>插值搜尋是針對均勻分佈排序數組的二分搜尋的改進變體。 </p> <p><strong>時間複雜度:</strong> 對於均勻分佈的數據,O(log log n),最壞情況下為 O(n)。 </p> <h3> 範例 6:插值搜尋實現 </h3> <pre class="brush:php;toolbar:false">function interpolationSearch(arr, target) { let low = 0; let high = arr.length - 1; while (low = arr[low] && target <h2> 5. 指數搜尋 </h2> <p>指數搜尋對於無界搜尋很有用,也適用於有界數組。 </p> <p><strong>時間複雜度:</strong> O(log n)</p> <h3> 範例 7:指數搜尋實現 </h3> <pre class="brush:php;toolbar:false">function exponentialSearch(arr, target) { if (arr[0] === target) { return 0; } let i = 1; while (i <h2> 6. 子數組搜索 </h2> <p>在較大數組中搜尋子數組是 DSA 中的常見問題。 </p> <h3> 範例 8:樸素子數組搜索 </h3> <p><strong>時間複雜度:</strong> O(n * m),其中 n 是主數組的長度,m 是子數組的長度。 <br> </p> <pre class="brush:php;toolbar:false">function naiveSubarraySearch(arr, subArr) { for (let i = 0; i <h3> 範例 9:用於子數組搜尋的 KMP 演算法 </h3> <p><strong>時間複雜度:</strong> O(n + m)<br> </p> <pre class="brush:php;toolbar:false">function kmpSearch(arr, pattern) { const n = arr.length; const m = pattern.length; const lps = computeLPS(pattern); let i = 0, j = 0; while (i <h2> 7. 兩指針技術 </h2> <p>兩指標技術通常用於在排序數組中搜尋或處理對時。 </p> <h3> 範例 10:尋找具有給定總和的對 </h3> <p><strong>時間複雜度:</strong> O(n)<br> </p> <pre class="brush:php;toolbar:false">function findPairWithSum(arr, target) { let left = 0; let right = arr.length - 1; while (left <h3> 例 11:三和問題 </h3> <p><strong>時間複雜度:</strong> O(n^2)<br> </p> <pre class="brush:php;toolbar:false">function threeSum(arr, target) { arr.sort((a, b) => a - b); const result = []; for (let i = 0; i 0 && arr[i] === arr[i - 1]) continue; let left = i + 1; let right = arr.length - 1; while (left <h2> 8. 滑動視窗技術 </h2> <p>滑動視窗技術對於解決具有連續元素的陣列/字串問題非常有用。 </p> <h3> 範例 12:大小為 K 的最大和子數組 </h3> <p><strong>時間複雜度:</strong> O(n)<br> </p> <pre class="brush:php;toolbar:false">function maxSumSubarray(arr, k) { let maxSum = 0; let windowSum = 0; for (let i = 0; i <h3> 範例 13:具有 K 個不同字元的最長子字串 </h3> <p><strong>時間複雜度:</strong> O(n)<br> </p> <pre class="brush:php;toolbar:false">function longestSubstringKDistinct(s, k) { const charCount = new Map(); let left = 0; let maxLength = 0; for (let right = 0; right k) { charCount.set(s[left], charCount.get(s[left]) - 1); if (charCount.get(s[left]) === 0) { charCount.delete(s[left]); } left++; } maxLength = Math.max(maxLength, right - left + 1); } return maxLength; } const s = "aabacbebebe"; console.log(longestSubstringKDistinct(s, 3)); // Output: 7
時間複雜度: O(log n)
function searchRotatedArray(arr, target) { let left = 0; let right = arr.length - 1; while (left = arr[left] && target arr[mid] && target <h3> 範例 15:在 2D 矩陣中搜尋 </h3> <p><strong>時間複雜度:</strong> O(log(m * n)),其中 m 是行數,n 是列數<br> </p> <pre class="brush:php;toolbar:false">function searchMatrix(matrix, target) { if (!matrix.length || !matrix[0].length) return false; const m = matrix.length; const n = matrix[0].length; let left = 0; let right = m * n - 1; while (left <h3> 範例 16:尋找峰值元素 </h3> <p><strong>時間複雜度:</strong> O(log n)<br> </p> <pre class="brush:php;toolbar:false">function findPeakElement(arr) { let left = 0; let right = arr.length - 1; while (left arr[mid + 1]) { right = mid; } else { left = mid + 1; } } return left; } const arr = [1, 2, 1, 3, 5, 6, 4]; console.log(findPeakElement(arr)); // Output: 5
時間複雜度: O(log n)
function searchUnknownSize(arr, target) { let left = 0; let right = 1; while (arr[right] <h3> 範例 18:找出旋轉排序數組中的最小值 </h3> <p><strong>時間複雜度:</strong> O(log n)<br> </p> <pre class="brush:php;toolbar:false">function findMin(arr) { let left = 0; let right = arr.length - 1; while (left arr[right]) { left = mid + 1; } else { right = mid; } } return arr[left]; } const rotatedArr = [4, 5, 6, 7, 0, 1, 2]; console.log(findMin(rotatedArr)); // Output: 0
時間複雜度: O(log n)
function searchRange(arr, target) { const left = findBound(arr, target, true); if (left === -1) return [-1, -1]; const right = findBound(arr, target, false); return [left, right]; } function findBound(arr, target, isLeft) { let left = 0; let right = arr.length - 1; let result = -1; while (left <h3> 範例 20:兩個排序數組的中位數 </h3> <p><strong>時間複雜度:</strong> O(log(min(m, n))),其中 m 和 n 是兩個陣列的長度<br> </p> <pre class="brush:php;toolbar:false">function findMedianSortedArrays(nums1, nums2) { if (nums1.length > nums2.length) { return findMedianSortedArrays(nums2, nums1); } const m = nums1.length; const n = nums2.length; let left = 0; let right = m; while (left minRightY) { right = partitionX - 1; } else { left = partitionX + 1; } } throw new Error("Input arrays are not sorted."); } const nums1 = [1, 3]; const nums2 = [2]; console.log(findMedianSortedArrays(nums1, nums2)); // Output: 2
為了進一步測試您對陣列搜尋的理解和技能,您可以練習以下 15 個 LeetCode 問題:
這些問題涵蓋了廣泛的陣列搜尋技術,並將幫助您鞏固對本部落格文章中討論的概念的理解。
總之,掌握數組搜尋技術對於精通資料結構和演算法至關重要。透過理解和實現這些不同的方法,您將能夠更好地解決複雜的問題並優化您的程式碼。請記住分析每種方法的時間和空間複雜度,並根據您問題的特定要求選擇最合適的一種。
祝您編碼和搜尋愉快!
以上是使用 JavaScript 在 DSA 中進行數組搜尋:從基礎到高級的詳細內容。更多資訊請關注PHP中文網其他相關文章!