leetcode34_Search for a Range
一.問題描述
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
二.程式碼編寫
對於有序序列,需求是對數時間複雜度的問題,第一反應就是二分法,對於這種有多個要求輸出的問題,與經典的二分查詢的不同就在於判斷條件多了許多,程式碼編寫的時候要非常細緻,比如在while大迴圈的判斷條件上,邊界是大於等於,而非大於。因為要找到兩端的兩個target的值,所以會有兩個左右指標,在指標移動時也要注意細節。這一題可以用迭代的思想來完成,會使程式碼不顯得這麼冗長,但效率上就不見得優於直接手寫了。程式碼如下:
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
len_nums = len(nums)
left1=left2 = 0 # 定義兩個指標對(left1,right1)和(left2,right2)
right1=right2 = len_nums-1
rlist = [-1,-1]
while left1<=right1 or left2<=right2:
if left1==left2 and right1 == right2: # 當兩個指標對相同時
mid = (left1+right1)/2
if nums[mid]>target:
right1=right2=mid-1
elif nums[mid]<target:
left1=left2=mid+1
else:
if mid>0 and nums[mid-1]==target:
right1=mid-1 # 前指標對繼續左移
else:
rlist[0] = mid
left1=-1 # 找到前target後立flag,以免後面程式碼繼續找
right1=-2
if mid<len_nums-1 and nums[mid+1]==target:
left2 = mid+1
else:
rlist[1]=mid
left2=-3 # 找到後target後立flag,以免後面程式碼繼續找
right2=-4
elif left1 != -1: # 兩個指標對不同且前指標對未找到最左邊的target
mid1 = (left1+right1)/2
if nums[mid1]<target:
left1=mid1+1
elif nums[mid1]>target:
right1 = mid1-1
elif mid1>0 and nums[mid1-1]==target:
right1 = mid1-1
else:
rlist[0] = mid1
left1=-1
right1=-2
elif left2 != -3: # 兩個指標對不同且後指標對未找到最右邊的target
mid2 = (left2+right2)/2
if nums[mid2]<target:
left2 = mid2+1
elif nums[mid2]>target:
right2 = mid2-1
elif mid2<len_nums-1 and nums[mid2+1]==target:
left2=mid2+1
else:
rlist[1]=mid2
left2=-3
right2=-4
return rlist
執行時間:
相關文章
- HTML input rangeHTML
- Python range()Python
- Range Sparse Net
- Convert Range-Partitioned Table To Interval-Range-Partitioned Table
- Golang for range的坑Golang
- MySQL RANGE分割槽MySql
- golang range的用法Golang
- MySQL range問題MySql
- Global Range Partitioned IndexesIndex
- B. Range and Partition
- python-rangePython
- Range Minimum Sum
- http斷點續傳原理:http頭 Range、Content-RangeHTTP斷點
- Go-For Range 效能研究Go
- The Range of Application2APP
- GO 的 range 如何使用?Go
- Leetcode Search for a RangeLeetCode
- Index Range Scan (214)Index
- ABAP Range 選擇表
- 為什麼range不是迭代器?range到底是什麼型別?型別
- Range/Content-Range與斷點續傳,瞭解一下?斷點
- Python range() 函式用法Python函式
- unsupported operand type(s) for +: ‘range‘ and ‘list‘
- Leetcode - Bitwise AND of Numbers RangeLeetCode
- LeetCode 34 Search for a RangeLeetCode
- LintCode-Search for a Range
- Leetcode-Search for a rangeLeetCode
- Search for a Range leetcode javaLeetCodeJava
- [AGC056B] Range ArgmaxGC
- Oracle分割槽表基礎運維-07增加分割槽(6RANGE_RANGE)Oracle運維
- CSS :out-of-range 選擇器CSS
- python入門:range函式Python函式
- Python錯題本(1)range()Python
- range與enumerate的區別
- LeetCode-Bitwise AND of Numbers RangeLeetCode
- input range美化程式碼例項
- 201-Bitwise AND of Numbers Range
- 理解HTML5中Range物件HTML物件