4 Pointer solution. Key: when moving pointers, we skip duplicated ones.

Ref: https://github.com/xbz/lintcode/blob/master/4_sum/4sum.cpp

class Solution {
void nextUnique(vector<int> &num, size_t &j)
{
while (j<num.size() && num[j]==num[j-]) ++j;
}
public:
vector<vector<int> > fourSum(vector<int> &num, int target)
{
vector<vector<int> > ret;
sort(num.begin(), num.end()); for (size_t i=; i<num.size(); ++i)
{
if(i > ) nextUnique(num, i); for (size_t j=i+; j<num.size(); ++j)
{
if(j>i + ) nextUnique(num, j); size_t m = j + ;
size_t n = num.size() - ;
while (m < n) {
int sum = num[i] + num[j] + num[m] + num[n];
if (sum == target)
{
vector<int> v = {num[i], num[j], num[m], num[n]};
ret.push_back(v);
m++;
n--; if(m>j+) nextUnique(num, m);
while (n<num.size()- && m<n && num[n]==num[n+]) n--;
} else if (sum < target)
++m;
else
--n;
}
}
}
return ret;
}
};

LintCode "4 Sum"的更多相关文章

  1. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  2. Lintcode: Subarray Sum 解题报告

    Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...

  3. LintCode Subarray Sum

    For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...

  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] Submatrix Sum 子矩阵之和

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

  6. Lintcode: Interval Sum II

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

  7. 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. ...

  8. Lintcode: Subarray Sum Closest

    Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...

  9. LintCode "Submatrix Sum"

    Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known eq ...

随机推荐

  1. web app 的技术参考 -- 来自 【百度移动建站指南】

    优化页面性能 考虑到移动设备和移动互联网的特点,在进行移动网站的页面开发设计时,一个总的原则是考虑用户访问的效率,降低页面加载时间.  下面的内容来自百度,然后我自己做了笔记.另外可参考这个系列的文章 ...

  2. 让Grub 2来拯救你的 bootloader

    没有什么事情比 bootloader 坏掉更气人的了,充分发挥 Grub 2 的作用,让 bootloader 安分工作吧.为什么这么说? Grub 2 是最受欢迎的 bootloader ,几乎用在 ...

  3. 计算机中如何表示数字-07IEEE754浮点数标准

    由于不同机器所选用的基数.尾数位长度和阶码位长度不同,因此对浮点数的表示有较大差别,这不利于软件在不同计算机之间的移植.为此,美国IEEE(电器及电子工程师协会)提出了一个从系统角度支持浮点数的表示方 ...

  4. HDU 5944 暴力

    Fxx and string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)T ...

  5. css selector

    文章一: http://www.jb51.net/css/68287.html 去年我学jQuery的时候,曾经做过一点选择器(selector)的笔记,今天是CSS的选择器,以后还有一部分xPath ...

  6. 第三课,T语言数据类型(版本TC5.0)

    数据类型 TC综合开发工具里使用的是可变类型,就是在程序执行过程中,TC综合开发工具会自动把数据转换为需要的类型,转换失败会出现相应的提示但是在一些特殊的场景下,是需要做类型强制处理,那么这时就需要使 ...

  7. 监听turtlesim仿真器,发送数据到实际的机器人--20

    摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.0.本教程教你写实际的ros程序,控制自己的机器人.采用的是PC端的ubuntu+ros.终 ...

  8. java多线程之:创建开启一个线程的开销

    ---->关于时间,创建线程使用是直接向系统申请资源的,这里调用系统函数进行分配资源的话耗时不好说.---->关于资源,Java线程的线程栈所占用的内存是在Java堆外的,所以是不受jav ...

  9. Flash pixel Bender学习笔记

    pixel Bender是指用来创建,编译,测试和导出pixel shader,用于各种flash产品的一个IDE. Pixel Bender是Adobe推出的一个高性能的图像视频处理技术.它能跨平台 ...

  10. jquery-easyui中表格的行编辑功能

    datagrid现在具有行编辑能力了,使用时只须在columns中为需要编辑的列添加一个editor属性,编辑保存时同时具有数据校验能力. 看一个例子效果图: 代码如下: $('#tt').datag ...