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. C语言开源项目

    值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...

  3. linux环境配置

    一.JDK安装 1.通过xftp工具把jdk-8u60-linux-x64.gz上传到linux 2.解压JDK命令tar -xzf jdk-8u60-linux-x64.gz 3.linux配置环境 ...

  4. Copying Fields to a new Record

    This is a time saving tip for application designer. If you are creating a new record definition and ...

  5. [leetcode]_Best Time to Buy and Sell Stock I && II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  6. CSS3 column-rule-style 属性

    CSS column-rule-style属性用于在多列布局中指定列与列之间通过column rule属性设置的分隔线的样式.column-rule是列与列之间的一条垂直分隔线,你可以使用column ...

  7. PHP+ajax聊天室源码!支持长轮循跟定时请求两种

      var lastID = "1";//声明上次取回的消息的ID var isposted = false; var mGetTime;//设置setTimeout的返回值 // ...

  8. php 递归 适合刚刚接解递归的人看

    递归,就是自己调用自己,当满足某条件时层层退出(后进先出). --------------------------------------------------------------------- ...

  9. Nob畅想在线教育

    1.社交网络的课堂实时互动 老师上课,每当和同学们互动时大家下边总是保持沉默,低着头,几乎每人拿着一部手机在看,还有pad等. 张星老师的课算是好一点,学生可以抬着头然后手下边捏着手机,时不时低头看一 ...

  10. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...