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. OpenHCI - 4.2 Endpoint Descriptor

    4.2 Endpoint DescriptorAn Endpoint Descriptor (ED) is a 16-byte, memory resident structure that must ...

  2. CentOS下Web服务器环境搭建LNMP一键安装包

    CentOS下Web服务器环境搭建LNMP一键安装包 时间:2014-09-04 00:50来源:osyunwei.com 作者:osyunwei.com 举报 点击:3797次 最新版本:lnmp- ...

  3. 总结 output 用法

    第一种用法 返回受 INSERT.UPDATE 或 DELETE 语句影响的每行的信息,或者返回基于上述每行的表达式.这些结果可以返回到处理应用程序, 以供在确认消息.存档以及其他类似的应用程序要求中 ...

  4. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. call & apply

    对于apply和call两者在作用上是相同的:这两个方法通常被用来类的继承和回调函数.但两者在参数上有区别的.call函数和apply方法的第一个参数都是要传入给当前对象的对象,及函数内部的this. ...

  6. UVa 839 天平

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. 《Java程序设计》第二次实验报告

    20145220 <Java程序设计>第二次实验报告 课程:Java程序设计 指导教师:娄嘉鹏 实验日期:2016.04.12 实验名称:Java面向对象程序设计 实验内容 初步掌握单元测 ...

  8. N皇后回溯解法 leetcode N-Queens

    class Solution { public: vector<vector<string> > solveNQueens(int n) { vector<vector& ...

  9. 利用caffe生成 lmdb 格式的文件,并对网络进行FineTuning

    利用caffe生成 lmdb 格式的文件,并对网络进行FineTuning 数据的组织格式为: 首先,所需要的脚本指令路径为: /home/wangxiao/Downloads/caffe-maste ...

  10. RandomAccessFile

    RandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,并进行读写了.这些记录的大小不必相同:但是其大小和位置必须是可知的.但是该类仅限于操作文件