Careercup - Facebook面试题 - 5761467236220928
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的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- Mybatis源码解析(一)(2015年06月11日)
一.简介 先看看Mybatis的源码结构图,Mybatis3.2.7版本包含的包共计19个,其他版本可能会少. 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为 ...
- 使用c#将多个文件放入文件夹中,并压缩下载
ZipClass.cs 这个是一个压缩文件的类,可直接复制使用,使用到的命名空间是 using System.IO;using ICSharpCode.SharpZipLib;using ICSha ...
- oracle生成.net的guid方法;
最近在做一个T1的.NET项目,数据库oracle的时候,遇到一个问题..NET里面的某个数据库表类的某个字段是guid类型.但是用oracle生成的guid.跟.NET的guid 无法识别.导致报错 ...
- 进程通信---FIFO
管道没有名字,所以只能在具有血缘关系的进程间使用,而在无名管道发展出来的有名管道FIFO,则有路径名与之相关联,以一种特殊设备文件形式存在于文件系统中,从而允许无亲缘关系的进程访问FIFO,下面看FI ...
- 一点总结-关于debug比赛
上午的题目是: 1. main里面定义的变量必须手动初始化,使用memset或者其他,函数外或者函数内,会进行初始化为0. 2. 最长回文子串的马拉车manacher算法,不会写! 3. 数字三角形d ...
- 【转】IL编织 借助PostSharp程序集实现AOP
ref: C# AOP实现方法拦截器 在写程序的时候,很多方法都加了.日志信息.比如打印方法开始,方法结束,错误信息,等等. 由于辅助性功能的代码几乎是完全相同的,这样就会令同样的代码在各个函数中 ...
- phpStudy 2016 更新下载,新版支持php7.0
目标:让天下没有难配的php环境. phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 『软件简介』该程序包集成 ...
- python杂记-2(python之文件)
文件打开函数:f = open 表1-1:open函数中模式参数常用值 打开模式 描述 'r' 读模式 'w' 写模式 'a' 追加模式 'b' 二进制模式 '+' 读/写模式 表1-2:文件对象方法 ...
- IE6和IE7的line-height和现代浏览器不一致的问题
1.我们发现在网页中设置line-height后,现代浏览器显示正常,可是在IE6 IE7下却不能正确解析,这时需要再额外的为旧版浏览器声明: p{ line-height: 30px; *line- ...
- XAML 概述四
这一节我们来简单介绍一下XAML的加载和编译,它包括如下三种方式: · 只使用代码 · 使用代码和未编译的XAML · 使用代码和编译过的BAML 一. 只使用代码 我们首先创建一个简单的控制台 ...