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. css优先机制

    样式的优先级 (外部样式)External style sheet <(内部样式)Internal style sheet <(内联样式)Inline style (内部样式就是css写在 ...

  2. 面对一个“丢失了与用户“签订”的协议的修改”时进行的思考。

    对于上图中的gauge,将value与label之间的比例值调整了,调整为1:1.2.这意味着,在新系统中打开老报表,老报表中的这个gauge的value可能会比以前大,二者可能是用户厌恶的效果. 严 ...

  3. Part 4 using entity framework

    Step1: Install entity framework using nuget package manager. Step2: Add a class file to the Models f ...

  4. 别人网站生成的json

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sb.ToString()); WebResponse rep = req.GetResp ...

  5. C# 匿名方法及Lambda表达式

    1.匿名方法 定义:匿名方法不能直接在类中定义,而是在给委托变量赋值的时候,需要赋值一个方法,此时可以“现做现卖”,定义一个匿名方法传递给该委托. 举实例说明: 实例一:无参数,无返回值的一个匿名方法 ...

  6. js 动态添加元素(div、li、img等)及设置属性

    把一串 html 标签赋给一个 javascript 变量,除属性的值要用转义的双引号外,某些时候字符串还很长,显得有些复杂.如果用 js 动态添加元素,就不会有那么复杂的字符串出现,代码阅读性强一点 ...

  7. Spring中Quartz的配置

    Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz: 首先,来写一个测试被调度的类:(QuartzHelloWorldJ ...

  8. 搭建Cocos2d-JS开发环境

    使用Cocos2d-JS引擎开发游戏,主要的程序代码是JavaScript语言,因此,凡是能够开发JavaScript语言工具都适用于Cocos2d-JS游戏开发.本书我们推荐WebStorm和Coc ...

  9. Firebug 调试技巧之console API

    console.log(object[, object, ...]) Writes a message to the console. You may pass as many arguments a ...

  10. 解析XML文档之一:使用SAX解析

    使用sax解析xml方法总结 解析的的xml文档格式如下 <?xml version="1.0" encoding = "UTF-8"?> < ...