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. April 23 2017 Week 17 Sunday

    It is a characteristic of wisdom not to do desperate things. 不做孤注一掷的事情是智慧的表现. We are told that we ha ...

  2. 154. Find Minimum in Rotated Sorted Array II(Binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...

  3. mongodb在C#的连接以及curd写法

    连接数据库:参考地址:https://blog.oz-code.com/how-to-mongodb-in-c-part-2/ // Empty ctor will get you a // clie ...

  4. UESTC 757 棋盘

    虽然是水题,但是还是很interesting的.(大概就是我最晚出这个题了... 博弈感觉就是靠yy能力啊.这题是对称性. 最后的必败态是白色格子对称的,一旦对称形成,对手怎么选,跟随就好,对手无法摆 ...

  5. 关于win10深度学习安装配置 CUDA9.0+VS2017+Cudnn7.4.1.5+Anaconda3(cupy安装包)+python3.7+pycharm

    0 查看电脑系统版本(非常重要) WIN+R->输入winver, 系统版本号必须高于1703,否则CUDA9.0难以运行!!!! 1 安装 NVIDIA 显卡驱动程序 下载地址:驱动程序 选择 ...

  6. Vuex基础-State

    官方地址:https://vuex.vuejs.org/zh/guide/state.html 由于 Vuex 的状态存储是响应式的,从 store 实例中读取状态最简单的方法就是在计算属性中返回某个 ...

  7. django2.2连接mysql遇到的坑

    1.mysql数据库配置 2.首先需要建一个myweb数据库 3.执行数据库迁移命令makemigrations python manage.py makemigrations MySite 报错: ...

  8. Linux实战教学笔记15:用户管理初级(下)

    第十四节 用户管理初级(下) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,用户查询相关命令id,finger,users,w,who,last,lastlog,gr ...

  9. 【Effective C++ 读书笔记】条款03: 尽量使用 const

    关键字const多才多艺,变化多端却不高深莫测. const 修饰指针 面对指针, 你可以指出 指针自身.指针所指物.或者两者都不是 const. 如果关键字 const 出现在星号左边,表示被指物是 ...

  10. eclipse引入jquery文件报错解决

    以下内容是小编给大家带来的关于Eclipse引入jquery报错如何解决的全部叙述,具体内容如下所示: 第一步: 去除eclipse的JS验证: 将windows->preference-> ...