求两链表元素对和为N的所有元素对
来源:互联网 发布:mac词典 编辑:程序博客网 时间:2024/05/18 01:08
有两个链表L1 ,L2.链表的链表的大小在十亿级。链表包含正数和负数。为了简便,假设他们都是整数。
给定整数N。现在找出L1+L2中的和为N的元素对。
L1 = 28, -7, 0, 56, 6, -8, 0, 72, 1000, -33
L2 = 53, 20, 27, -52, 99, 14, -8
N = 20
The answer will be:
(28, -8), (-7, 27), (0, 20), (6, 14), (0, 20), (72, -52)
You do have two linked list L1 and L2. The size of linked lists is huge and in billions. Linked List contains numbers (both negative and positive). For simplicity you can assume they are all integers.
You have been given a number say N. now you need to find out all of the pairs where one element from L1 + one element from L2 = N.
i.e.
L1 = 28, -7, 0, 56, 6, -8, 0, 72, 1000, -33
L2 = 53, 20, 27, -52, 99, 14, -8
N = 20
The answer will be:
(28, -8), (-7, 27), (0, 20), (6, 14), (0, 20), (72, -52)
假设不考虑重复,
list1 = 10, 10
list2 = 20
target N = 30,
只需要输出(10, 20)
如果list的大小很小,可以使用hash表来解决问题。关键是链表大小是十亿级的。有将近4十亿个整数,需要16GB的内存,如果我们将其全部存储在内存中--太大了。
我们可以使用bitmap:使用4十亿个bit去表示这些整数,话费512MB内存。如果整数在list1中出现,设置对应bit位为1.映射如下:使用前29bit计算偏移量,使用最后3bit计算应设置的bit位。000设置第0位为1,111设置第七位为1...(一个整数有32位)
使用这种密集的方式存储list1,对于list2的每一个元素去检查在list1中有没有对应的元素使得和为N。
以上就是线性时间的解法。
I am assuming that duplication does not matter: say
list1 = 10, 10
list2 = 20
target N = 30,
then we only need to output (10, 20). We do not need to output (10, 20), (10, 20)
If the list sizes are small, everyone knows the solution based on hash_set. The tricky part here is the list size (billions of integers). There are 4 billion integers, which requires 16GB memory if we store them in the memory ---- too large.
We can play the trick if bitmap encoding: we use 4 billion bits to represent the integers, which takes 512MB (much more reasonable now). If an integer appears in list1, we set the corresponding bit. The mapping is like this: calculate the byte offset using the first 29 bits, then calculate the bit to set using the last 3 bits (an integer has 32 bits in total).
After we store list1 using this compact way, we can check every element in list2 to see if there is an element in list1 to make the sum equal to N.
This is a linear time solution.
- 求两链表元素对和为N的所有元素对
- 在给定的数组中找出两个元素和为给定值的所有元素对
- 在给定的数组中找出两个元素和为给定值的所有元素对
- 20141008个人日志(空数组添加元素不报错,快速找出不成对出现的元素,随机生成和为S的N个正整数)
- float对内联元素和块元素的影响
- CSS3 transform对普通元素的N多渲染影响
- CSS3 transform对普通元素的N多渲染影响
- CSS3 transform对普通元素的N多渲染影响
- C++ STL 算法:对所有元素排序
- STL算法 -------- 对所有元素排序
- Python 对字典元素的赋值 (字典key和value均为组合类型)
- 用JAVA编写一个算法实现对一个字符数组的所有元素的所有组合
- 数组配对 给定N个整数,N为偶数,是否能找到N/2对,使得每对和能被K整除。注意:每个元素只能出现在一个配对中。public class num9 { public boolean checkP
- 有 M X N的矩阵,若行某行某列为0,则置0元素所在行列所有元素为0?
- 数组元素的值为其他所有元素的累积
- 从长度为N的数组中找出所有M个元素组合的优化算法
- list.toArray() 和 Arrays.asList() 对 null 元素的处理
- jQuery对表单元素的取值和赋值操作
- Unix 输入和输出
- 用js判断页面刷新或关闭的方法(好)
- 快速阅读1, 1,阅读两遍比一遍更快,三遍更好,首遍略读,领略其大意,第二遍再快速阅读找重点,第三遍把文章各个点串起来,明确主题思想。
- HttpClient 知识自学
- 冒泡排序法,cpp实现
- 求两链表元素对和为N的所有元素对
- 适配器模式(Adapter)
- UVA 10986 By ACReaper
- Shark:强大的开源C++机器学习库
- php面试题之填空题001
- ps如何(怎么)调整色阶的原理和方法
- 成长与教训——软件工程实验小结<二>
- 整理:Linux网络编程之sockaddr与sockaddr_in,sockaddr_un结构体详细讲解
- linux的文件描述符集fd_set的实现方式