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. 安卓手机下载YouTube视频的3种方法

    作为全球最大的在线视频网站,YouTube上面的内容可真是应有尽有啊,从教学视频到个人手工艺品制作流程,从各种搞笑视频到电视连续集等等,包罗万象.如果你想下载YouTube视频到电脑上面的话,网上有很 ...

  2. MySQL入门很简单: 12 MYSQL 用户管理

    1. 权限表 安装MySQL会自动安装一个名为mysql的数据库,存储权限表: user表, db表,host表,table_priv表,columns_priv表,proc_priv表等. 1)us ...

  3. Recent plan + Summary (two weeks)

    Plan: Homework: B365 (next week) B392, B335 Interview: Friday, do the assignment Thursday Summary: I ...

  4. ios 创建自己的.a文件

    1:首先创建个 静态工程(Cocoa Touch Static Library); 方法名字,一定要暴露在.h文件中, 2:分别在模拟器环境和真机环境下 Analyze (shift+command+ ...

  5. vim 中的":wq"和":x"的区别

    ":x" 和 ":wq" 的区别如下:(1) :wq 强制性写入文件并退出(存盘并退出 write and quite).即使文件没有被修改也强制写入,并更新文 ...

  6. 统计函数运行时间-CPU端

    C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下:  clock_t clock( void ); 这个函数返回从“开启这个程序 ...

  7. javascript中的作用域与作用域链

    前几天,在写一段js代码时,出现了一些问题,调了很长时间也没有调通,其原因是,我在处理变量的作用域时错误地沿用了C++的作用域机制.因此我回炉了一次. 如果你使用过C++或java等一系列的面向对象的 ...

  8. 开发者不容错过的10款免费JavaScript游戏引擎

    摘要:使用HTML5.JavaScript可以帮助开发者开发出各种与众不同的游戏及游戏特效,比如3D动画.Canvas等.本文介绍10款被广泛使用的基于HTML5的JavaScript游戏引擎. 在G ...

  9. iPad游戏 Calcculator: The Game 程序自动计算求解方法

    今天在iPad上下了个小游戏,主要是一个计算器的界面,有开始值,目标值,限定步数,以及一些加减乘除,还有作者脑洞想出来的功能键,主要有左移,直接把一个数加到末尾,将其中的某个数改为另一个数等等..玩到 ...

  10. Spring/Spring boot中静态变量赋值

    情形1:静态变量为自动注入的对象 解决方案:设置两个变量,非静态变量使用@resource注入Bean,然后使用@PostConstruct在Spring初始化Bean成功后为静态变量赋值 @Comp ...