如何可以写一个算法,以检查是否任何两个数中的阵列/列表的总和给定数量的匹配
与 nlogn
?
How can I write an algorithm to check if the sum of any two numbers in an array/list matches a given number
with a complexity of nlogn
?
我敢肯定有一个更好的办法,但这里有一个想法:
I'm sure there's a better way, but here's an idea:
排序数组 对于每一个元素的电子的阵中,二进制搜索为补充的(总和 - E)的 Sort array For every element e in the array, binary search for the complement (sum - e)这两种操作为O(n log n)的
。