《Cracking the Coding Interview》——第17章:普通题——题目12
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- cracking the coding interview系列C#实现
原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录 1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目14
2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
随机推荐
- JSON:json_encode函数不能获取属性原因及解决方案
json_encode()是个解析json数据的函数,但是这个函数可以有两个参数 形式: json_decode ( string $json, ture || false ) 第一个参数传字 ...
- 使用g++ 编译C++程序
在命令行下,编译C++程序 g++ main.cpp -o main.exe
- linux中CURL的安装
curl是一款著名的字符界面下的下载工具,支持HTTP.HTTPS.FTP.FTPS.DICT.TELNET.LDAP.FILE,和GOPHER.此外还具有cookies支持.断点续传.FTP上传.密 ...
- php curl使用总结(一)
今天和第三方支付做对接的时候,在本地用wamp(php版本5.4.14)运行他们的支付demo的时候,报了一个错误.loadXML函数中不能传空值.排查代码的时候,发现他们用了curl,我以前也接触过 ...
- 大数据(1)初始hadoop
1.hadoop模型如下: (上图为Hadoop1.x的布局) (Hadoop2.x较Hadoop1.x,多了YARN) Hadoop框架,是一个庞大的生态系统. 或者我们可以这样理解: 可以把整个体 ...
- HTML5 input date 移动端 IOS 不支持问题
1.placeholder 问题解决方法 对 input type date 使用 placeholder 的目的是为了让用户更准确的输入日期格式,iOS 上会有 date 不会显示 placehol ...
- 实现一个clone函数,对javascript中的5种数据类型进行值复制
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JQuery 过滤选择器 与属性修改的方法演示比较
文本匹配 在表单输入项里面输入值,根据输入值,点击判断按钮,让对应的复选框选中 <html> <head> <meta http-equiv="Content- ...
- cocos2dx 修改亮度、对比度、色调、饱和度
废话少说,直接修改CCSprite使用的片面着色器ccShader_PositionTextureColor_noMVP.frag: /* * cocos2d for iPhone: http://w ...
- socket传送二进制流的一些总结
第一次实质性的接触socket通信方面的工作,所以遇到的问题还真不少,写篇博客记录一下,提升下记忆. 需求是通过私有协议进行二进制数据的传输,必须保证数据包不能被丢失,所以选择tcp的socket进行 ...