Let's begin with a naive method.

We first need to sort the array A[n]. And we want to solve the problem by iterating through A from beginning and ending. Then, if the sum is less than the target, we move the leading pointer to next right. When the sum is larger than target, we move the ending pointer to next left. The workflow of finding a, b such that $$a + b = target$$ as flows:

vector<vector<int> > res;

//we access the array from start point and end point
int* begin = A;
int* end = A + n - 1; while(begin < end){
if(begin + *end < target)//it means we need to increase the sum
begin += begin; if(begin + *end > target)//it means we need to decrease the sum
end -= end; if(begin + *end == target){
begin += begin;
end -= end;
res.push_back({*begin, *end}); // there may be some other combinations
++begin;
--end;
}
}

Running Time:

  • $O(n*\log{n})$ for sorting.
  • $O(n)$ for accessing through the array

In fact, there're some directly optimizations. When we move the pointer begin and end, it will stay the same status if $$ *(new\ begin) == *begin $$, or $$ *(new\ end) == *end$$. Thus, we can move the pointers until it reaches the first different value.

++begin;
while(begin < length && num[begin] == num[begin-1])
++begin;

and

--end;
while(end > 0 && num[end+1] == num[end])
--end;

Assume we have m same *begin, n same *end, we will reduce the running time of iterating moving points from $O(m*n)$ to $O(m+n)$.

Pay attention the above analysis and optimization are only useful when we find valid combination.

  • When $*begin + *end == target$. In this case, we need to move both begin and end. Thus we reduce running time from $O(m*n)$ to $O(m+n)$.
  • When $*begin + *end < target$, we only do m times ++begin. And when we get different begin, we stop. Without the optimization, the loop process is the same. So in this case, we only move the begin. The running time is always $O(m)$.
  • When $*begin + *end > target$, we have the same deduction. In this case, we only move end. The running time is always $O(n)$.

The Three Sum problem is based on the Two Sum problem above. In the Three Sum prolem, the direct optimization talked above is very important.

If we don't need to implement the three sum problem, we can use the hash table to get $O(n)$ running time.

TwoSum / Three Sum的更多相关文章

  1. LeetCode题解——Two Sum

    题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers ...

  2. the sum of two fixed value

    the sum of two fixed value description Input an array and an integer, fina a pair of number in the a ...

  3. 【leetcode】633. Sum of Square Numbers(two-sum 变形)

    Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...

  4. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  5. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  6. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  7. [LeetCode] Two Sum 两数之和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  8. leecode系列--Two Sum

    学习这件事在任何时间都不能停下.准备坚持刷leecode来提高自己,也会把自己的解答过程记录下来,希望能进步. Two Sum Given an array of integers, return i ...

  9. LeedCode-Two Sum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

随机推荐

  1. activiti如何获取当前节点以及下一步路径或节点(转)

    ACTIVITI相对于JBPM来说,比较年轻,用的人少,中文方面的资料更少,我根据网上到处找得资料以及看官方文档总结出来了代码,非常不容易啊.废话不多说,直接上代码吧: 首先是根据流程ID获取当前任务 ...

  2. UVA 10821 Constructing BST

    BST: binary search tree. 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...

  3. java链表实现

    import java.util.Scanner; class DATA2 { String key; // 结点的关键字 String name; int age; } class CLType / ...

  4. 2017/2/8 hibernate + oracle 实现id的自增 同时 hibernate项目跑起来 会自己增加字段的原因 oracle触发器的使用

    hibernate + oracle 实现id的自增 1.在oracle中先创建一个序列 : 序列语法 如下 create  sequence   (序列名称)seq_student_id minva ...

  5. Java在dos界面运行java源文件编译成功,但运行虚拟机时出现错误:“找不到或无法加载主类”的问题

    (一)首先检查环境变量配置有没有问题, 1PATH为%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 2CLASSSPATH为.;%JAVA_HOME%\lib\dt.jar; ...

  6. JSON文件导入Unity3d中是空的的问题

    将Json文件的内容在网上在线的Json文件编辑器导出后再导入即可

  7. windows中当你的键盘无法使用时我们可以用另一种方法哦

    1.使用Win+R打开cmd窗口 2.输入osk回车就出现了一个虚拟的小键盘啦,当你的键盘坏掉后非常实用哦

  8. 2019.01.04 bzoj2962: 序列操作(线段树+组合数学)

    传送门 线段树基础题. 题意:要求维护区间区间中选择ccc个数相乘的所有方案的和(c≤20c\le20c≤20),支持区间加,区间取负. 由于c≤20c\le20c≤20,因此可以对于每个线段树节点可 ...

  9. DOM3级的变化

    由于存在跨浏览器开发问题所以不推荐使用: 兼容性: event.key 包含所按下键的字符 event.char 属性IE9和safari和chrome并不支持 event.location 返回所按 ...

  10. Java交流分享(522818473)

    今天来分享哈自己的技术交流群,记得还是刚接触Java建立的群,那时候学习Java很有动力,经常和群友谈论问题,现在都专注公司业务和技术这一块,很多后端框架都没用除了restful,其他都是封装的,不过 ...