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 ...
随机推荐
- 【转】eclipse集成开发工具的插件安装
转发一:打开Eclipse下载地址(http://www.eclipse.org/downloads/),可以看到有好多版本的Eclipse可供下载,初学者往往是一头雾水,不知道下载哪一个版本. 各个 ...
- (基础篇)PHP字符串操作
PHP是弱类型语言,所以其它类型的数据一般可以直接应用于字符串操作函数里,而自动转换成字符串类型,进行处理,如: echo substr("1234567", 1, 3); 和 ...
- raido 赋值第一次成功,然后就赋值不显示
$("#id").attr("checked",true); //显示出现问题,第一次成功 $("#id").prop("chec ...
- ArrayList笔记
elementData是一个缓存数组,它通常会预留一些容量,等容量不足时再扩充容量,这个楼主一定是明白的,不用多解释.假如现在实际有了5个元素,而elementData的大小可能是10,那么在序列化时 ...
- android 多线程下载图片
很多时候我们需要在Android设备上下载远程服务器上的图片进行显示,今天Android123整理出两种比较好的方法来实现远程图片的下载. 方法一.直接通过Android提供的Http类访问远程服 ...
- phpmyadmin使用中碰到的一些问题
在导入数据库文件的时候出现 #1062 – Duplicate entry '1′ for key ‘PRIMARY' 说明在上一次的导入中没有完全导入,但是主键是自增的,所以要输入主键才能继续,解决 ...
- HZAU 17:LCS
17: LCS Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 184 Solved: 43[Submit][Status][Web Board] De ...
- 附录三 嵌入式C程序的编译与调试
课程回顾 C语言库的特性和发展 C语言库的常用库函数 标准库函数的特色应用 git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysi ...
- 15款Chrome浏览器插件让设计师告别拖延症
秋高气爽,分享一大波有效帮助设计师提高工作效率的Chrome浏览器扩展程序! 高效是另一种王道 无论是在工作中,还是在生活中,有些词我们说来就满满正能量,而另外一些话提起就很沮丧,后者如拖延症,前者如 ...
- 如何用 matlab 在图片上绘制矩形框 和 添加文字 ?
如何给图像添加矩形框?以及添加想要输入的文字 ? 案例程序,如下所示: clc; close all; clear all;image = imread('/home/wangxiao/Picture ...