演算法 二分查詢 (陣列)

2021-09-25 19:43:46 字數 3171 閱讀 3846

我們把符合下列屬性的陣列 a 稱作山脈:

a.length >= 3

存在 0 < i < a.length - 1 使得a[0] < a[1] < … a[i-1] < a[i] > a[i+1] > … > a[a.length - 1]

給定乙個確定為山脈的陣列,返回任何滿足 a[0] < a[1] < … a[i-1] < a[i] > a[i+1] > … > a[a.length - 1] 的 i 的值。

示例 1:

輸入:[0,1,0]

輸出:1

示例 2:

輸入:[0,2,1,0]

輸出:1

3 <= a.length <= 10000

0 <= a[i] <= 10^6

a 是如上定義的山脈

class solution(object):

def peakindexinmountainarray(self, a):

""":type a: list[int]

:rtype: int

"""return a.index(max(a))

給定兩個陣列,編寫乙個函式來計算它們的交集。

示例 1:

輸入: nums1 = [1,2,2,1], nums2 = [2,2]

輸出: [2]

示例 2:

輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

輸出: [9,4]

說明:輸出結果中的每個元素一定是唯一的。

我們可以不考慮輸出結果的順序。

class solution(object):

def intersection(self, nums1, nums2):

""":type nums1: list[int]

:type nums2: list[int]

:rtype: list[int]

"""dic = {}

p=for num in nums1:

if num not in dic:

dic[num] = 0

for num in nums2:

if num in dic:

del dic[num]

return p

有序的陣列nums:

class solution(object):

def search(self, nums, target):

""":type nums: list[int]

:type target: int

:rtype: int

"""left ,right = 0, len(nums)-1

while left <= right:

mid=(left + right)/2

if nums[mid] == target:

return mid

elif nums[mid] < target:

left = mid + 1

else:

right = mid - 1

return -1

給定乙個已按照公升序排列 的有序陣列,找到兩個數使得它們相加之和等於目標數。

函式應該返回這兩個下標值 index1 和 index2,其中 index1 必須小於 index2。

說明:返回的下標值(index1 和 index2)不是從零開始的。

你可以假設每個輸入只對應唯一的答案,而且你不可以重複使用相同的元素。

示例:輸入: numbers = [2, 7, 11, 15], target = 9

輸出: [1,2]

解釋: 2 與 7 之和等於目標數 9 。因此 index1 = 1, index2 = 2 。

class solution(object):

def twosum(self, numbers, target):

""":type numbers: list[int]

:type target: int

:rtype: list[int]

"""low,high = 0,len(numbers)-1

while(low < high):

if (numbers[low] + numbers[high] == target):

return [low+1, high+1]

elif numbers[low] + numbers[high] 給定乙個排序陣列和乙個目標值,在陣列中找到目標值,並返回其索引。如果目標值不存在於陣列中,返回它將會被按順序插入的位置。

你可以假設陣列中無重複元素。

示例 1:

輸入: [1,3,5,6], 5

輸出: 2

示例 2:

輸入: [1,3,5,6], 2

輸出: 1

示例 3:

輸入: [1,3,5,6], 7

輸出: 4

示例 4:

輸入: [1,3,5,6], 0

輸出: 0

class solution(object):

def searchinsert(self, nums, target):

""":type nums: list[int]

:type target: int

:rtype: int

"""low,high = 0,len(nums)

while low target:

high=mid

elif nums[mid] 給定兩個陣列,編寫乙個函式來計算它們的交集。

示例 1:

輸入: nums1 = [1,2,2,1], nums2 = [2,2]

輸出: [2,2]

示例 2:

輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

輸出: [4,9]

說明:輸出結果中每個元素出現的次數,應與元素在兩個陣列**現的次數一致。

我們可以不考慮輸出結果的順序。

高階:如果給定的陣列已經排好序呢?你將如何優化你的演算法?

如果 nums1 的大小比 nums2 小很多,哪種方法更優?

如果 nums2 的元素儲存在磁碟上,磁碟記憶體是有限的,並且你不能一次載入所有的元素到記憶體中,你該怎麼辦?

陣列 二分查詢演算法

二分查詢演算法 二分查詢演算法也稱為折半查詢法,它的思想是每次都與序列的中間元素比較。二分查詢的乙個前提條件是陣列是有序的,假設陣列array為遞增序列,finddata為要查詢的數,n為陣列長度,首先將n個元素分成個數大致相同的兩半,取array n 2 與將要查詢的值finddata進行比較,如...

bisect 陣列二分查詢演算法

這個模組對有序列表提供了支援,使得他們可以在插入新資料仍然保持有序。對於長列表,如果其包含元素的比較操作十分昂貴的話,這可以是對更常見方法的改進。這個模組叫做 bisect 因為其使用了基本的二分 bisection 演算法。源 也可以作為很棒的演算法示例 邊界判斷也做好啦!定義了以下函式 bise...

bisect 陣列二分查詢演算法

bisect.bisect left a,x,lo 0,hi len a 如果 x 已經在 a 裡存在,那麼插入點會在已存在元素之前 也就是左邊 如果 a 是列表 list 的話,返回值是可以被放在 list.insert 的第乙個引數的 返回的插入點 索引 i 可以將陣列 a 分成兩部分。左側是 ...