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. spark+hcatalog操作hive表及其数据

    package iie.hadoop.hcatalog.spark; import iie.udps.common.hcatalog.SerHCatInputFormat; import iie.ud ...

  2. Java 内部类和匿名类 实现JButton动作 ActionListener类

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ControlCircle2 extend ...

  3. JS基础知识(基本类型 引用类型)

    1,js中的  基本类型 引用类型  javascript中有两种变量类型:基本类型和引用类型,基本类型包括:Number.String.Undefined.Null.Boolean这五种,而引用类型 ...

  4. Intellij IDEA 创建Web项目并在Tomcat中部署运行

      一.创建Web项目 1.File -> New Module,进入创建项目窗口 2.选择Java类型,在 Module name 处输入项目名,点击Next 3.勾选 Web Applica ...

  5. Ubuntu 13.10下Hadoop 2.2 安装、配置、编译(伪分布式)

    1.安装JDK.在此不做解说,上篇博文里已介绍过.http://www.cnblogs.com/lifeinsmile/p/3578677.html 2.配置ssh. ssh服务,用于管理远程Hado ...

  6. Java 性能优化实战记录(3)--JVM OOM的分析和原因追查

    前言: C/C++的程序员渴望Java的自由, Java程序员期许C/C++的约束. 其实那里都是围城, 外面的人想进来, 里面的人想出去. 背景: 作为Java程序员, 除了享受垃圾回收机制带来的便 ...

  7. 使用Matlab对灰度图像编程实现2D的傅里叶变换

    1.      先载入一幅灰度图像,如下: (非灰度图) 2. 利用函数fft2,对其进行快速傅立叶变换, 并利用函数fftshift 将变换后的图像原点移动到频率矩形的中心. 3. 利用abs()函 ...

  8. java如何获取当前机器ip和容器port

    获取当前机器ip: private static String getIpAddress() throws UnknownHostException { InetAddress address = I ...

  9. JavaWeb学习记录(二)——防盗链技术

    public class TestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSer ...

  10. puppet安装配置及使用

     puppet安装前准备 一.服务器信息 master端:10.10.10.201 master.fansik.com slave端:10.10.10.156 slave.fansik.com 三台机 ...