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. javaSE第十四天

    第十四天    92 1. 正则表达式(理解)    92 (1)定义:    92 (2)常见规则    92 A:字符    92 B:字符类    93 C:预定义字符类    93 D:边界匹 ...

  2. C# 集合 — Hashtable 线程安全

    基础知识重要吗?真的很重要. 就在笔者与同事聊天中突然同事提出一个问题,让笔都有点乱了手脚(有点夸张),题目是这样的: 问:Hashtable 是线程安全的吗? 答:…… (沉默中,Yes Or No ...

  3. Win2003打不开https的问题

    碰到客户做问题是能打开https://www.baidu.com 这个网页 打不开 https://sha256.alipay.com/SHA256/index.htm支付宝这个网页      解决办 ...

  4. 数据包判断是否丢包 ping+tracert+mtr

    1.用咱们最常用的Ping命令来查看是不是真的丢包了 这里可以看到数据包发送了4个,返回了4个,丢失=0  证明没有丢失 也有可能中间路由做了策略不给ICMP的回应 这样就ping没法判断了  正常情 ...

  5. Regarding the %EDIT table

    %EDITTABLE is field in the work record DERIVED. This field is generally used a prompt table for vari ...

  6. 在PeopleSoft系统中实现打印页面的功能

    我们知道,在PeopleSoft HCM里,一般上了薪酬模块的话,都会客户化工资单页面,去匹配公司之前的工资单的报表的格式.有的时候,这个工资单页面又需要打印出来,以供员工的使用.PeopleSoft ...

  7. 第一节 MongoDB介绍及下载与安装

    引言 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类 ...

  8. mac下,利用homebrew安装PHP+Mysql+Nginx

    具体可以参考直通车 http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-home ...

  9. Hao123这个流氓

    Author:KillerLegend Date:2014.2.27 From:http://www.cnblogs.com/killerlegend/p/3572591.html Hao123真让人 ...

  10. [.ashx檔?泛型处理例程?]基础入门#2....FileUpload上传前,预览图片(两种作法--ashx与JavaScript)

    原文出處  http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/20/ashx_beginner_02_fileupload_picture_p ...