题目:输入一个已经按升序排序过的数组和一个数字,

在数组中查找两个数,使得它们的和正好是输入的那个数字。

要求时间复杂度是 O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。

例如输入数组 1、2、4、7、11、15 和数字 15。由于 4+11=15,因此输出 4 和 11。

分析:

给出两种算法: 1. 枚举第一个,然后二分搜索第二个数。 O(NlgN)

2. 双指针法。 left, right 分别指向首尾,如果 a[left] + a[right] < sum, left++,  如果 >, right --; 否则 找到答案

扩充:找出 3个数,使其和等于 sum. 见 点击打开链接

// copyright @ L.J.SHOU Mar.05, 2014
// 2 sum
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; // O(NlgN)
void TwoSumVersion1(vector<int> &a, int sum)
{
if(a.size() < 2)
return; for(vector<int>::iterator it = a.begin(); it != a.end(); ++it)
{
if(binary_search(a.begin(), it, sum - *it)
|| binary_search(it+1, a.end(), sum - *it))
{
cout << *it << " " << sum - *it << endl;
return;
}
}
} // two pointers O(N)
void TwoSumVersion2(vector<int> &a, int sum)
{
vector<int>::iterator left(a.begin()), right(a.end()-1); while(left < right)
{
if(*left + *right < sum){
++ left;
}
else if(*left + *right > sum){
-- right;
}
else{
cout << *left << " " << *right << endl;
return;
}
}
} int main(void)
{
int a[]={1,2,4,7,11,15};
vector<int> vec(a, a+sizeof(a)/sizeof(int)); TwoSumVersion1(vec, 15);
TwoSumVersion2(vec, 15); return 0;
}

Interview----2 sum的更多相关文章

  1. 行列转换文本处理--awk xargs 回顾

    awk 数组回顾: 9.1 数组 举例:统计当前主机上每一个TCP连接状态以及每种连接状态的数目[非常实用] # netstat -tan | awk '/^tcp/{STATE[$NF]++}END ...

  2. Amazon Interview | Set 27

    Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...

  3. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  4. [LintCode] Two Sum 两数之和

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  5. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  6. [LintCode] Submatrix Sum 子矩阵之和

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

  7. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  8. k Sum | & ||

    k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...

  9. Lintcode: Interval Sum II

    Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...

  10. Lintcode: Interval Sum

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

随机推荐

  1. 手机页面head中的meta元素

    <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="ex ...

  2. asp.net 使用UrlRewritingNet.UrlRewriter组件URL重写,伪静态详解

    目录 URL重写的业务需求 ReWritingNet组件主要功能 配置IIS(IIS7/8环境下) 程序代码 重写规则 一,URL重写的业务需求 顾客可以直接用浏览器bookmark功能将页面连结储存 ...

  3. BZOJ1029: [JSOI2007]建筑抢修(贪心)

    题目链接:BZOJ1029: [JSOI2007]建筑抢修 题解:贪心思想,按结束时间从小到大排序,选花费时间尽量短的建筑维修,用堆维护. #include<stdio.h> #inclu ...

  4. 获取txt文件指定行内容

    #!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1;  #表示行 ...

  5. node 日志管理log4js

    node 日志管理log4js 一.默认的控制台输出 我们使用express框架时,开发模式用node或者supervisor启动nodejs应用时,控制台都是显示如下的日志. GET /css/bo ...

  6. git在公司内部的使用实践(转)

    从2011.10月左右,开始在后台组推行git版本控制,到现在也差不多半年了,也形成了一套基于git flow的副官模式工作流程: 版本定义: 版本号使用x.x.x进行定义,第一个x代表大版本只有在项 ...

  7. easyui中的combobox小知识点~~

    一直使用的easyui中,一些不为人知的小知识点,与君共勉: 1.combobox设置高度:使用panelHeight属性: 2.combobox本身自带“自动补全”功能,但是在浏览器中是有限制的,在 ...

  8. 46. Permutations——本质和树DFS遍历无异 fun: for i in nums fun(i)

    Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have ...

  9. 445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  10. formvalidator4.1.3 使用过程中一些问题的解决

    在使用formvalidator4.1.3 插件时  发现 正常情况调用时没有问题的,但是我们的项目中需要把css  .js 之类的静态资源用单独的域名加载. 那么问题就来了在 该插件中 有一段这样的 ...