在给定的数组中找出两个元素和为给定值的所有元素对

23 篇文章 0 订阅
Design an algorithm to find all pairs of integers within an array which sum to

a specified value. 


使用hash map:

1假设V为给定的值,A为给定的数组。

2创建hash map M,M将从数组元素映射到出现次数。

3对数组中的元素A[i]:

如果 V-A[i] 在M中,打印A[I] 和V-A[I], M[V-A[i]] 次.

如果A[i]在M中,增加M[A[i]],否则M[A[i]] = 1.

算法能处理例如两个元素相等以及重复元素的特殊例子。 (V/2, V/2) (e.g. V=7 and A=[3,4,3,4]).

SOLUTION
One easy and (time) efficient solution involves a hash map from integers to integers. This
algorithm works as follows:
1. Let be V be the specified value, and let A be the array.
2. Create a hash map, M, which will map from array elements to occurrence counts.
3. For each element A[i] in the array:
If V-A[i] is present in M, then print A[I] and V-A[I], M[V-A[i]] times.
If A[i] is present in M, increment M[A[i]], otherwise let M[A[i]] = 1.
This algorithm conveys the special cases such as identical pairs (V/2, V/2) and repeated values

(e.g. V=7 and A=[3,4,3,4]).


另外一种解法:

补充的定义:如果要找出和为z的两个元素,x的补充就为z-x。

算法:假设数组已经是排好序的。让first指向数组头,last指向数组尾。如果 first + last < sum,表示first没有补充,first前移。如果 first + last > sum,last后移。如果 first + last == sum first++  last--. 当first比last大时停止移动。

Alternate Solution
Definition of Complement: If we’re trying to find a pair of numbers that sums to z, the complement
of x will be z - x (that is, the number that can be added to x to make z). For example,
if we’re trying to find a pair of numbers that sum to 12, the complement of –5 would be 17.
The Algorithm: Imagine we have the following sorted array: {-2 -1 0 3 5 6 7 9 13 14 }. Let first
point to the head of the array and last point to the end of the array. To find the complement
of first, we just move last backwards until we find it. If first + last < sum, then there is no complement
for first. We can therefore move first forward. We stop when first is greater than last.
Why must this find all complements for first? Because the array is sorted and we’re trying progressively
smaller numbers. When the sum of first and last is less than the sum, we know that
trying even smaller numbers (as last) won’t help us find a complement.
Why must this find all complements for last? Because all pairs must be made up of a first and

a last. We’ve found all complements for first, therefore we’ve found all complements of last.


//Implementation:
#include <iostream>
#include <conio.h>
#include <algorithm>
using namespace std;
void print_pairs(int * ptr, int num, int sum) {
	std::sort(ptr, ptr + num);
	int first = 0;
	int last = num - 1;
	while (first < last) {
		int s = ptr[first] + ptr[last];
		if (s == sum) {
			cout << ptr[first] << “ “ << ptr[last] << endl;
			++first;
			--last;
		} else {
			if (s < sum) {
				++first;
			} else {
				--last;
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值