leetcode33_Search in Rotated Sorted Array
一.問題描述
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4
5 6 7 0 1 2
).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
二.演算法實現
其實還是二分查詢的思想,只是在指標移動上多了很多需要判斷的條件。具體程式碼如下:
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
# 還是二分查詢的思想,只是指標移動的條件有所改變
len_nums = len(nums)
left = 0
right = len_nums-1
mid = (left+right)/2
while left<=right:
if nums[mid] == target:
return mid
elif nums[left] == target:
return left
elif nums[right] ==target:
return right
elif nums[left]<nums[mid]: # 說明左側list是遞增的
if target>=nums[left] and target<=nums[mid]:
right = mid
mid = (left+mid)/2
else:
left = mid
mid = (right+mid)/2
elif target>=nums[mid] and target<=nums[right]:
left = mid
mid = (mid+right)/2
if mid == left:
return -1
else:
right = mid
mid = (mid+left)/2
if mid == right:
return -1
return -1
程式碼寫的很爛,好多ifelse判斷(因為想的時候沒考慮到所有情況吧,提交了好多次,一直在打補丁- -),最後一次提交的時候心驚膽戰的,怕時間打敗了0.001%的人,結果提交的時候發現打敗了92%的人,又驚又喜又嚇的- -,但是演算法在寫法上感覺還是不太美好,好多冗餘判斷的感覺,有待繼續提升。
優化了一下,每次左右指標移動不是移動到mid處,而是mid加減1處。如下:
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
len_nums = len(nums)
left = 0
right = len_nums-1
mid = (left+right)/2
while left<=right:
if nums[mid] == target:
return mid
elif nums[left] == target:
return left
elif nums[right] == target:
return right
elif nums[left]<nums[mid]:
if target>=nums[left] and target<=nums[mid]:
right = mid-1
mid = (left+right)/2
else:
left = mid+1
mid = (right+left)/2
elif target>=nums[mid] and target<=nums[right]:
left = mid+1
mid = (left+right)/2
else:
right = mid-1
mid = (right+left)/2
return -1
提交了兩次,第一次是打敗了19%,嚇一跳,再提交一次就變成98%了,- -。。。相關文章
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted Array IILeetCode
- Leetcode-Search in Rotated Sorted ArrayLeetCode
- Search in Rotated Sorted Array leetcode javaLeetCodeJava
- LeetCode-Search in Rotated Sorted Array IILeetCode
- Search in Rotated Sorted Array II leetcode javaLeetCodeJava
- Find Minimum in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode-Find Minimum in Rotated Sorted Array IILeetCode
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode 153. Find Minimum in Rotated Sorted ArrayLeetCode
- LeetCode C++ 33. Search in Rotated Sorted Array【二分】中等LeetCodeC++
- [LeetCode] Search in Rotated Sorted Array 在旋轉有序陣列中搜尋LeetCode陣列
- [CareerCup] 11.3 Search in Rotated Sorted Array 在旋轉有序矩陣中搜尋矩陣
- [LeetCode] Find Minimum in Rotated Sorted Array (包含遞增和遞減旋轉)LeetCode
- Remove-duplicates-from-sorted-arrayREM
- 26. Remove Duplicates from Sorted ArrayREM
- Leetcode Merge Sorted ArrayLeetCode
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode-Merge Sorted ArrayLeetCode
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode 88. Merge Sorted ArrayLeetCode
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Convert Sorted Array to BSTLeetCode
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Median of Two Sorted Array leetcode javaLeetCodeJava
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- Leetcode-Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted Array II leetcode javaREMLeetCodeJava
- Leetcode 34 Find First and Last Position of Element in Sorted ArrayLeetCodeAST
- Leetcode Convert Sorted Array to Binary Search TreeLeetCode
- 【LeetCode 88_陣列】Merge Sorted ArrayLeetCode陣列
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- [LeetCode] 702. Search in a Sorted Array of Unknown SizeLeetCode