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. OpenStack实战(一)

    OpenStack作为当前发展势头迅猛的云计算开源项目,去年进行了一些了解,现在有空回来进行一些补充记录,当时实战的版本是那会最新版本,当然现在已经更新了好几版了,不过还是那句话“这些丝毫不影响,了解 ...

  2. IOS多线程(一)

    一.绪论 1.进程:平时看到的一个应用程序,即可算作一个线程. 每个进程都有一个PID作为进程ID,有一个Process Name作为进程名字等. 2.线程:一个进程可以有多个线程,而每个线程只可属于 ...

  3. c#学习之Socket网络编程

    我是新手以前没写过博客 希望大家勿喷, 在编写Socket的时候需要导入System.Net.Socket 命名空间.利用该类我们可以直接编写Socket的客户端和服务的的程序了, 这里我们只讲tpc ...

  4. silverlight获取web的url参数

    1.网址(如:http://localhost:8081/index.aspx?name=123) 2.获取name=123的信息 3.IDictionary<string,string> ...

  5. SDUST作业10 Problem J: 提取缩略词

    Description 在英文文献中,尤其是专业文献中,经常有很多的缩略词,如CPU代表Central Processing Unit等.为了方便学习,Qili决定从一批英文论文中提取出所有的缩略词以 ...

  6. 利用js+canvas实现的时钟效果图

    canvas+js时钟特效 运用js+canvas方面的知识完成一个时钟的效果图,再利用for循环实现指针的转动效果: <!--网页文档的声明--> <!doctype html&g ...

  7. Linux基础知识-文件管理

    Linux目录与路径 cd:切换目录 例如:cd ~willhua,则回到用户willhua的主文件夹  cd ~或者cd,则表示回到自己的的主文件夹  cd -,则表示回到上个目录 pwd:显示目前 ...

  8. 【php学习之路】php基础语法

    一.什么是php?       PHP即PHP: Hypertext Preprocessor(超文本处理器),是一种服务器端脚本语言,适用于创建web站点.开源免费 二.php能做什么?       ...

  9. php中关于抽象(abstract)类和抽象方法的问题解析

    在面向对象(OOP)语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法作为外部代码访问的接口.而抽象方法就是为了方便继承而引入的,现在来看一下抽象类和抽象方法分别是如何定义以及他们的特点 ...

  10. 向Windows 日志管理器写入系统程序日志信息

    标准样例代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...