2014-05-02 07:06

题目链接

原题:

Given an array of randomly sorted integers and an integer k, write a function which returns boolean True if a pair of numbers exists in the array such that A[i] + A[j] = k and False otherwise. Provide an O(N) and an O(N log N) solution.

题目:给定一个未排序的数组,找出是否存在A[i] + A[j]等于某一个值。请给出一个O(n)的解法和一个O(n * log(n))的解法。

解法1:如果是O(n)解法的话,可以在扫描过程中逐渐向哈希表里添加数组元素,并同时查找target - A[i]是否存在于哈希表中。所谓的O(n)也是理想化的,条件是哈希过程中没有冲突。

代码:

 // http://www.careercup.com/question?id=5761467236220928
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std; class Solution {
public:
bool findTwoSum(vector<int> &v, int target) {
int n = (int)v.size();
unordered_set<int> us; if (n < ) {
return false;
} int i;
for (i = ; i < n; ++i) {
if (us.find(target - v[i]) != us.end()) {
us.clear();
return true;
} else {
us.insert(v[i]);
}
} us.clear();
return false;
};
}; int main()
{
int i;
int n;
vector<int> v;
int target;
Solution sol; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
}
cin >> target;
cout << (sol.findTwoSum(v, target) ? "True" : "False") << endl;
v.clear();
} return ;
}

解法2:可以通过先将数组排序,然后从两端向中间进行一次扫描。这个做法在Leetcode题集中的Two Sum已经提到了。排序过程是O(n * log(n))的,扫描则是O(n)的。

代码:

 // http://www.careercup.com/question?id=5761467236220928
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std; class Solution {
public:
bool findTwoSum(vector<int> &v, int target) {
int n = (int)v.size();
unordered_set<int> us; if (n < ) {
return false;
} int i;
for (i = ; i < n; ++i) {
if (us.find(target - v[i]) != us.end()) {
us.clear();
return true;
} else {
us.insert(v[i]);
}
} us.clear();
return false;
};
}; int main()
{
int i;
int n;
vector<int> v;
int target;
Solution sol; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
}
cin >> target;
cout << (sol.findTwoSum(v, target) ? "True" : "False") << endl;
v.clear();
} return ;
}

Careercup - Facebook面试题 - 5761467236220928的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. Mac OSX系统搭建React natvie for android 开发环境

    1.下载node.js  https://nodejs.org/en/ 下载OSX版本 V5.6  2.安装android SDK  JDK  3.安装react-native-cli 打开命令行,输 ...

  2. hdu 六度分离 floyd

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 题意分析:比较简单的最短路算法,最后只需判断最远两点距离是否大于7即可. /*六度分离 Time ...

  3. js获取url及url参数的方法

    <script language="JavaScript" type="text/javascript"> function GetUrlParms ...

  4. HDU 5024 Wang Xifeng's Little Plot(枚举)

    题意:求一个图中只有一个90°拐点的路的最大长度. 分析:枚举每一个为'.'的点,求出以该点为拐点的八种路中的最大长度,再比较所有点,得出最大长度即可. 如上样例,这样是个90°的角... 注意:最多 ...

  5. UILabel常用属性

    [super viewDidLoad]; // 实例化UILabel并指定其边框 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0 ...

  6. Windows Phone 7 ListBox 列表项渐显加载动画学习笔记

    在wp7程序中,当程序功能越来越复杂时,性能问题是我们不得不考虑的一个问题.在聊天列表中,如果聊天项过多,而且项目UI组件足够复杂时, 我们不得不想尽办法让UI尽快加载.所以有一种可行的方案,就是像Q ...

  7. 使用JDBC从数据库中查询数据

    * ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...

  8. eclipse juno版本中没用 ant

    下载了谷歌提供的Android集成开发工具ADT,里面封装了Eclipse,但是很奇怪的是竟然没有Ant插件在里面 标准的Eclipse一般都是内置集成了Ant的. 然后到eclipse的plugin ...

  9. 解决Discuz“完善用户资料”任务不能完成的问题

    最近用 Discuz X3.2 搭建了个论坛,在测试过程中发现"完善用户资料"这个官方自带的任务有个Bug,将所有的资料都填写完成后,任务仍然无法完成,而且没有明确提示有哪些项目没 ...

  10. php读取html并截取字符串

    一般php采集代码能用的到,这里只是简单的实现代码. <?php $title='脚本学堂'; $hello='jbxue.com!'; $file=file_get_contents('htt ...