每日程式設計題 10

2021-10-25 15:16:18 字數 4399 閱讀 5720

所有題目均採用python進行完成

def

binary_search

(num, target)

:# 判斷是否為空

iflen

(num)==0

:return-1

left =

0 right =

len(num)-1

while left +

1< right:

mid =

(left + right)//2

if num[mid]

== target:

right = mid

elif num[mid]

< target:

left = mid

elif num[mid]

> target:

right = mid

# 判斷邊界條件

if num[left]

== target:

return left

if num[right]

== target:

return right

return

-1

關鍵是找到拐點

def

search

(num):if

len(num)==0

:return-1

left =

0 right =

len(num)-1

while left +

1< right:

if num[left]

< num[right]

:return num[left]

mid = left +

(right - left)//2

if num[left]

<= num[mid]

: left = mid +

1else

: right = mid

return num[left]

if num[left]

< num[right]

else num[right]

核心和上乙個程式一樣

def

search_target

(num, target):if

len(num)==0

:return-1

left, right =0,

len(num)-1

while left +

1< right:

mid = left +

(right - left)//2

if num[mid]

== target:

return mid

if num[left]

< num[mid]

:if num[left]

<= target and target <= num[mid]

: right = mid

else

: left = mid

else

:if num[mid]

<= target and target <= num[right]

: left = mid

else

: right = mid

if num[left]

== target:

return left

if num[right]

== target:

return right

return

-1

給定有序陣列和目標值,如果找到目標數則返回索引值,沒有找到就返回按順序應該被插入的位置的索引

def

search_insert_position

(num, target):if

len(num)==0

:return

0 left, right =0,

len(num)-1

while left +

1< right:

mid = left +

(right - left)//2

if num[mid]

== target:

return mid

if num[mid]

> target:

right = mid

else

: left = mid

if num[left]

>= target:

return left

if num[right]

>= target:

return right

return right +

1

給定乙個有序陣列,找到給定目標的開始和結束位置

思路:找到第一次出現目標和最後一次出現目標的位置,寫兩個二分法

def

search_area

(num, target):if

len(num)==0

:return(-

1,-1

)# left bound

left, right =0,

len(num)-1

while left +

1< right:

mid = left +

(right - left)//2

if num[mid]

== target:

right = mid

elif num[mid]

> target:

right = mid

else

: left = mid

if num[left]

== target:

lbound = left

elif num[right]

== target:

lbound = right

else

:return(-

1,-1

)# right bound

left, right =0,

len(num)-1

while left +

1< right:

mid = left +

(right - left)//2

if num[mid]

== target:

left = mid

elif num[mid]

> target:

right = mid

else

: left = mid

if num[left]

== target:

rbound = left

elif num[right]

== target:

rbound = right

else

:return(-

1,-1

)return

(lbound, rbound)

時間複雜度 o(n)

首先確定左邊 or 右邊第乙個字元的位置

def

search_empty

(num, target):if

len(num)==0

:return-1

left, right,=0

,len

(num)-1

while left +

1< right:

while left +

1< right and left =='':

if num[left]

==''

: left +=

1if right < left:

return-1

mid = left +

(right - left)//2

if num[mid]

== target:

return mid

if num[mid]

< target:

left = mid +

1else

: right = mid -

1if num[left]

== target:

return left

if num[right]

== target:

return right

return

-1

每日程式設計題 03

所有題目實現都是採用python 在乙個m行n列的矩陣中,如果查詢到乙個元素為 0 將其所在的行和列的元素都設為 0 需要考慮乙個記錄矩陣,一般都會考慮m行n列的,但是可以行列分開考慮,這樣會減少空間複雜度,只需要 m n def myzero matrix m none len matrix n ...

每日程式設計題 06

所有題目還是採用python進行解答的哦!今天的題目相對來說比較簡單,也有很多種解決方法,我只是給出了一些方法,大家可以多多查查,希望一起交流學習。def forsolve n for迴圈 s 1if n 0 return 1else for i in range 1 n 1 s i return ...

每日程式設計題 11

題目均採用python進行完成 已知供暖裝置和房屋的位置,給出供暖裝置的最小半徑 注意 供暖裝置需要排序 可以使用python中的bisect找到需要插入的索引值 from bisect import bisect deffind radius house,heater 供暖裝置排序 heater....