前言
這題主要運用到二分搜尋法,是 704. Binary Search 的變化題,目標是找到一個旋轉陣列中指定元素的陣列,用到一個 while 迴圈和其餘 if 判斷,時間複雜度估 O(log n),這裡有 JAVA 和 Python 的寫法。
題目
There is an integer array
numssorted in ascending order (with distinct values).
Prior to being passed to your function,numsis possibly rotated at an unknown pivot indexk(1 <= k < nums.length) such that the resulting array is[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](0-indexed). For example,[0,1,2,4,5,6,7]might be rotated at pivot index3and become[4,5,6,7,0,1,2].
Given the arraynumsafter the possible rotation and an integertarget, return the index oftargetif it is innums, or-1if it is not innums.
You must write an algorithm withO(log n)runtime complexity.
有一個整數陣列
nums已升序排序 (且具不同的值)。
在傳遞給你的函數之前,nums可能在一個未知的樞軸索引k(1 <= k < nums.length) 處旋轉,這樣得到的陣列是[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](索引從0開始),例如,[0,1,2,4,5,6,7]可能會在樞軸 3 處旋轉並變成[4,5,6,7,0,1,2]。
給定一個可能的旋轉後的陣列nums和一個整數target,如果target它在nums中就回傳target的索引, 如果它不在nums中就回傳-1。
你必須寫出一個O(log n)時間複雜度的演算法。
題目連結:https://leetcode.com/problems/search-in-rotated-sorted-array/
題目限制
1 <= nums.length <= 5000104 <= nums[i] <= 104
All values of nums are unique.nums is an ascending array that is possibly rotated.104 <= target <= 104
範例
1 | Input: nums = [4,5,6,7,0,1,2], target = 0 |
1 | Input: nums = [4,5,6,7,0,1,2], target = 3 |
1 | Input: nums = [1], target = 0 |
思路&筆記
這題用的技巧是二分搜尋法,原理是每次循環都會將陣列搜索範圍縮小一半,目的是要找到 target 值的 index
- 初始我們會設定起點位置、終點位置、中間點 (這些是二分搜尋法的必要條件)
middle = start + ( end - start ) / 2可取得中間點- 如果整個陣列都是有序的,那麼目標整數 target 必然存在於該有序的部分,我們可以直接進行二分搜尋找到目標值,但這題比較麻煩的是我們不知道陣列會旋轉成什麼樣子,所以先判斷陣列的左邊還是右邊是否有序
- 如果那半邊是有序的,就在那半邊使用二分搜尋法做搜尋
- 如果 target 不在那半邊的話,則會轉向另一邊做搜尋
- 直到 start 大於等於 end,表示搜尋範圍為空,結束搜尋
- 到最後範圍會縮到最小,起點值和目標值一定會重疊,然後回傳答案索引
[注意點] 之所以要用上述中間點的寫法會比較安全,因如果 start 和 end 趨近於最大數時,兩者相加起來的合可能會造成整數溢位
JAVA 實現
1 | class Solution { |
Python 實現
使用了和 Java 一樣的邏輯執行,換成 Python 的寫法
不一樣的點是變數的宣告方式不一樣,還有 Java 和 Python 的三元運算寫法不一樣
1 | class Solution: |
成績
| Language | Runtime | Memory |
|---|---|---|
| Java | 0ms | 41.2MB |
| Python | 54ms | 16.75MB |