LintCode "4 Sum"
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"的更多相关文章
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- 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 ...
- [LintCode] Two Sum 两数之和
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LintCode] Submatrix Sum 子矩阵之和
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...
- Lintcode: Interval Sum II
Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...
- 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. ...
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- 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 ...
随机推荐
- jquery zclip 复制黏贴功能不能实现
按照http://www.steamdev.com/zclip/我实现一个简单的zclip test 以下是我的测试code: <!DOCTYPE html> <html> & ...
- 如何让Visual Studio 清除最近打开项目 关闭上次未关闭的标签窗口
删除最近打开的文件: 打开HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\FileMRUList 删除最近打开的项目: 打开HKEY_CUR ...
- Windows IP安全策略。
一直在遗憾Windows下没有一个如Linux小Iptables一样的工具,能够严格的管控机器的访问限制. 后面突然看到一个叫Ipsec在Windows感觉还不错.以命令行的方式进行定义的话在多台服务 ...
- 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【POJ3904】【P1202】水晶密码
说是莫比乌斯反演,其实只是玩儿玩儿内个miu函数而已…… 原题: wty 打算攻击 applepi 的用来存放机密数据的水晶系统. applepi 早有察觉,于是布置了一个密码系统来防备 wty ...
- Subversion服务器搭建
如何快速建立Subversion服务器,并且在项目中使用起来,这是大家最关心的问题,与CVS相比,Subversion有更多的选择,也更加的容易,几个命令就可以建立一套服务器环境,可以使用起来,这里配 ...
- php get set方法深入理解
在类当中,设计通用的set和get方法,可以简化对属性的读写,这种方法不同于针对于独立的属性的普通的get和set方法,后者针对每个属性,都必须提供一对方法,前者针对所有属性,因此,可以看作是批量定义 ...
- Java并发编程-并发工具包(java.util.concurrent)使用指南(全)
1. java.util.concurrent - Java 并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.concurrent 包.这个包包含有一系列能够让 Ja ...
- MySQL 5.1.73升级为MySQL 5.5.35详解
一.前言 二.概述 三.安装MySQL 5.1.73 四.升级为MySQL 5.5.35 五.总结 注,测试环境 CentOS 6.4 x86_64,MySQL 版本(5.1.73.5.5.35)目前 ...
- python--类方法、对象方法、静态方法
1.我们已经讨论了类/对象可以拥有像函数一样的方法,这些对象方法与函数的区别只是一个额外的self变量 # -*- coding:utf-8 -*- #!/usr/bin/python # Filen ...