2014-05-06 13:23

题目链接

原题:

Finding a pair of elements from two sorted lists(or array) for which the sum of the elements is a certain value. Anyway solution that can do better than O(a.length + b.length)?

题目:给定两个有序的数组,如何从两数组中各选出一个元素使得两元素加起来等于某个目标值。

解法:又是这个“Guy”出的题目,此人想要追求优于O(n + m)的算法。这人代码水平不高,我想他对于算法复杂度的上下界也不知道怎么估计吧。我个人认为不太可能更优化了,因为你找的不是一个元素,而是一对。我的解法,是使用两个iterator,一个在A数组头部,一个在B数组尾部。通过A数组后移,B数组前移来调整相加的结果。这样的算法,复杂度就是O(n + m)的。

代码:

 // http://www.careercup.com/question?id=6271724635029504
#include <cstdio>
#include <vector>
using namespace std; bool twoSortedArraySum(vector<int> &a, vector<int> &b, int target, int &ia, int &ib)
{
int i, j;
int na, nb; na = (int)a.size();
nb = (int)b.size(); i = ;
j = nb - ; int sum;
while (i <= na - && j >= ) {
sum = a[i] + b[j];
if (sum > target) {
--j;
} else if (sum < target) {
++i;
} else {
ia = i;
ib = j;
return true;
}
}
return false;
} int main()
{
vector<int> a, b;
int na, nb;
int i;
int ia, ib;
int target; while (scanf("%d%d", &na, &nb) == && (na > && nb > )) {
a.resize(na);
b.resize(nb);
for (i = ; i < na; ++i) {
scanf("%d", &a[i]);
}
for (i = ; i < nb; ++i) {
scanf("%d", &b[i]);
}
while (scanf("%d", &target) == ) {
ia = ib = -;
if (twoSortedArraySum(a, b, target, ia, ib)) {
printf("%d + %d = %d\n", a[ia], b[ib], target);
} else {
printf("Not found.\n");
}
}
} return ;
}

Careercup - Google面试题 - 6271724635029504的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. 查看cics 运行状态

     查看cics 运行状态cicscp -v status all 

  2. php通过正则从字符串中获取所有图片url地址

    /** * 提取字符串中图片url地址 * @param type $str * @return type */ function getimgs($str) { $reg = '/((http|ht ...

  3. [原]Python 简单文件处理

    仅仅是为了Linux操作方便= =命令行最近没有时间仔细看看,电脑一直都在机房,暂且这般记着吧= = spath="D:/download/baa.txt" f=open(spat ...

  4. wget下载FTP的文件

    在Linux中我们怎么样实现wget来下载文件  下面例子 下载所有的py结尾的文件到当前目录 wget ftp://anymous:anymous@42.51.152.2/soft/*.py 递归的 ...

  5. js中settimeout方法加参数

    js中settimeout方法加参数的使用. 简单使用看w3school  里面没有参数调用,  例子: <script type="text/javascript"> ...

  6. win7修改软件【授权给…】后面的名称

    很简单,按下图打开注册表,修改: Ctrl+F分别搜索以下三个键值,搜到后修改为你想显示的名称即可即可. 1:UserName 2:RegisteredOrganazition 3:Registere ...

  7. 5.css字体

    下面的用一个表格总结了文本样式中字体的一些设置方法: 属性名 说明 CSS 版本 font-size 设置字体的大小 1 font-variant 设置英文字体是否转换为小型大写 1 font-sty ...

  8. Java当中的I/O的字符流

    字符流读写文件时,以字符为基础 I/O当中字符流的核心类 Reader类和Writer类是所有字符流类的父类,同样也是抽象类.FileReader和FileWriter分别是它们的子类. 核心类的核心 ...

  9. C 实现一个跨平台的定时器 论述

    引言 今天我们要讲述和构建的是一个跨平台多线程C的定时器对象,粒度是毫秒级别.可以称之为简易的timer, sctimer.h 库. 首先看总的接口,看门见客. sctimer.h #ifndef _ ...

  10. 菜鸟学习Struts——Scope属性

    一.概念. 在Action映射配置中,Scope属性可以取值为:request或session.Scope属性表示:Struts框架在将     ActionForm对象(与目标Action匹配的Ac ...