设计一个算法找到一个阵列中的所有整数对该笔款项到指定的值 [英] Design an algorithm to find all pairs of integers within an array which sum to a specified value
本文介绍了设计一个算法找到一个阵列中的所有整数对该笔款项到指定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
设计一个算法找到一个阵列中的所有整数对该笔款项到指定的值。
Design an algorithm to find all pairs of integers within an array which sum to a specified value.
我已经使用哈希表来存储数组元素的总和项试过这个问题,但它不是一个有效的解决方案。我可以使用哪些算法来有效地解决这个问题?
I have tried this problem using a hash table to store entries for the sum of array elements but it is not an efficient solution. What algorithm can I use to solve this efficiently?
推荐答案
假设所需金额= R
- 排序数组
- 在数组A(n)的每个数字,做一个二进制搜索找到数A(X),使得A(N)+ A(X)= R
这篇关于设计一个算法找到一个阵列中的所有整数对该笔款项到指定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文