33. 搜索旋轉(zhuǎn)排序數(shù)組(C++)
整數(shù)數(shù)組?nums
?按升序排列,數(shù)組中的值?互不相同?。
在傳遞給函數(shù)之前,nums
?在預(yù)先未知的某個下標(biāo)?k
(0 <= k < nums.length
)上進(jìn)行了?旋轉(zhuǎn),使數(shù)組變?yōu)?[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]
(下標(biāo)?從 0 開始?計數(shù))。例如,?[0,1,2,4,5,6,7]
?在下標(biāo)?3
?處經(jīng)旋轉(zhuǎn)后可能變?yōu)?[4,5,6,7,0,1,2]
?。
給你?旋轉(zhuǎn)后?的數(shù)組?nums
?和一個整數(shù)?target
?,如果?nums
?中存在這個目標(biāo)值?target
?,則返回它的下標(biāo),否則返回?-1
?。
你必須設(shè)計一個時間復(fù)雜度為?O(log n)
?的算法解決此問題。
?
示例 1:
輸入:nums = [4,5,6,7,0,1,2]
, target = 0輸出:4
示例?2:
輸入:nums = [4,5,6,7,0,1,2]
, target = 3輸出:-1
示例 3:
輸入:nums = [1], target = 0輸出:-1
?
提示:
1 <= nums.length <= 5000
-104 <= nums[i] <= 104
nums
?中的每個值都?獨(dú)一無二題目數(shù)據(jù)保證?
nums
?在預(yù)先未知的某個下標(biāo)上進(jìn)行了旋轉(zhuǎn)-104 <= target <= 104
刷題代碼
本地調(diào)試代碼
標(biāo)簽: