![](/img/trans.png)
[英]How to compare each element in two arrays with time complexity less than O(n^2)
[英]Compare elements of two arrays in less than O(n^2) time?
我有兩個整數數組。 我需要找出兩個數字,每個數組一個,其總和等於2.這在O(n ^ 2)中非常簡單,但有沒有辦法更快地完成它?
你可以在O(N + M)時間和O(N)空間中這樣做:
a
元素放入哈希集中 b
,檢查哈希表是否包含2-b[i]
構造N
元素的散列集需要O(N)時間和O(N)空間。 針對散列集檢查每個M
元素需要O(1),總共O(N + M)個時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.