2014-04-29 00:04

题目:给定一个整数数组,找出所有加起来为指定和的数对。

解法1:可以用哈希表保存数组元素,做到O(n)时间的算法。

代码:

 // 17.12 Given an array of integers and target value, find all pairs in the array that sum up to the target.
// Use hash to achieve O(n) time complexity. Duplicates pairs are skipped.
#include <cstdio>
#include <unordered_map>
#include <vector>
using namespace std; int main()
{
vector<int> v;
unordered_map<int, int> um;
int n, i;
int x, y;
int target; while (scanf("%d", &n) == && n > ) {
scanf("%d", &target); v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
} // duplicate pairs are skipped
for (i = ; i < n; ++i) {
um[v[i]] = um[v[i]] + ;
} unordered_map<int, int>::iterator it, it2;
for (it = um.begin(); it != um.end(); ++it) {
x = it->first;
y = target - x;
if (x > y) {
continue;
} --it->second;
if ((it2 = um.find(y)) != um.end() && it2->second > ) {
printf("(%d, %d)\n", x, y);
}
++it->second;
} v.clear();
um.clear();
} return ;
}

解法2:先将数组排序,然后用两个iterator向中间靠拢进行扫描。总体时间是O(n * log(n))。

代码:

 // 17.12 Given an array of integers and target value, find all pairs in the array that sum up to the target.
// O(n * log(n) + n) solution.
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; int main()
{
vector<int> v;
int n, i;
int ll, rr;
int target; while (scanf("%d", &n) == && n > ) {
scanf("%d", &target); v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
}
sort(v.begin(), v.end());
ll = ;
rr = n - ; int sum;
while (ll < rr) {
sum = v[ll] + v[rr];
if (sum < target) {
while (ll + < rr && v[ll] == v[ll + ]) {
++ll;
}
++ll;
} else if (sum > target) {
while (rr - > ll && v[rr] == v[rr - ]) {
--rr;
}
--rr;
} else {
printf("(%d, %d)\n", v[ll], v[rr]);
while (ll + < rr && v[ll] == v[ll + ]) {
++ll;
}
++ll;
}
} v.clear();
} return ;
}

《Cracking the Coding Interview》——第17章:普通题——题目12的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. cracking the coding interview系列C#实现

    原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录   1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...

  10. 《Cracking the Coding Interview》——第17章:普通题——题目14

    2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...

随机推荐

  1. powershell远程连接

    最近因为工作的需要看了看powershell相关的知识,个人总结了一点有关于powershell远程连接需要做的步骤,希望对别人有所帮助. 使用powershell远程连接,需要进行 设备的配置: 1 ...

  2. PS:将一个图片变成圆形

    1.选择一张正方形图片并放置到PS软件中 2.选择“选框工具”->右击选择"椭圆选框工具” 3.画出你要圆形内的范围: 4.选择两种方式中的一种,实际上效果是一毛一样的 5.右下角就会 ...

  3. hdu-2838 Cow Sorting---逆序对的花费

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2838 题目大意: 就是求将之前的排列变成一个递增的排列,每交换两个数的代价为两个数的和,求变成递增的 ...

  4. object dection资源

    https://handong1587.github.io/deep_learning/2015/10/09/object-detection.html

  5. Adobe CS2提供免费序列号

    据Adobe官方博客报道,自2012年12月13日起,因为技术故障,该公司已停止使用Creative Suite(CS2)产品及Acrobat 7的激活服务器. 这些产品大多是7年前发布,很多已经无法 ...

  6. 旧文备份:对象字典0x1005和0x1006的理解

    SYNC不一定由主站产生,因此,产生SYNC的节点,0x1005对象的值一般是0x40000080,第30位为1表示本节点产生 SYNC,而本节点的0x1006对象就是产生同步周期值了;而接收SYNC ...

  7. 第50章 读写内部FLASH—零死角玩转STM32-F429系列

    第50章     读写内部FLASH 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...

  8. java、javac -version不一致(java编译及运行环境不一致)的环境变量设置问题解决

    问题描述: 电脑上同时安装了JDK1.6与1.7  设置了环境变量JAVA_HOME为jdk1.6.0_21的安装目录,并且在PATH变量中加入了%JAVA_HOME%\bin,但在Windows命令 ...

  9. BZOJ1050: [HAOI2006]旅行comf(并查集 最小生成树)

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4021  Solved: 2257[Submit][Status][Discuss] Descript ...

  10. Java分享笔记:使用keySet方法获取Map集合中的元素

    /*--------------------------- Map集合中利用keySet方法获取所有的元素值: ....keySet方法:将Map中的所有key值存入到Set集合中, ....利用Se ...