【4Sum】cpp
题目:
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
- Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
- The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
代码:
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target)
{
vector<vector<int> > result;
sort(num.begin(), num.end());
unsigned int len = num.size();
if (len<) return result;
for (int i = ; i < len-; ++i)
{
if ( i> && num[i]==num[i-] ) continue;
for (int j = len-; j>i+; --j)
{
if ( j<len- && num[j]==num[j+] ) continue;
int k = i+;
int z = j-;
while(k<z)
{
const int tmp_sum = num[i]+num[j]+num[k]+num[z];
if (tmp_sum==target)
{
vector<int> tmp;
tmp.push_back(num[i]);
tmp.push_back(num[k]);
tmp.push_back(num[z]);
tmp.push_back(num[j]);
result.push_back(tmp);
++k;
while ( num[k]==num[k-] && k<z ) ++k;
--z;
while ( num[z]==num[z+] && k<z ) --z;
}
else if (tmp_sum>target)
{
--z;
while ( num[z]==num[z+] && k<z ) --z;
}
else
{
++k;
while ( num[k]==num[k-] && k<z ) ++k;
}
}
}
}
return result;
}
};
Tips:
1. 上面的代码时间复杂度O(n³)并不是最优的,网上有一些其他的可能做到O(n²)用hashmap的方式。
2. 上面的代码沿用了3Sum一样的思想:
a. 3Sum需要固定一个方向的变量,头尾各设定一个指针,往中间逼近。
b. 4Sum由于多了一个变量,则需要固定头并且固定尾,在内部的头尾各设定一个指针,再往中间逼近。
3. TwoSum 3Sum 4Sum这个系列到此为止了 套路基本就是固定头或尾的变量 再往中间逼
=================================================
第二次过这个题目,一开始想到了要固定头尾的思路,再在中间采用2Sum的算法。两个原因没有成行:
1. 看到了O(n³)超时的说法,没敢写。。。
2. 可能是第一次AC就是学的这种写法,有印象
但是,第二次AC代码并不是上述的思路,而是采用了一种类似万能的写法。
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> > ret;
if ( nums.size()< ) return ret;
vector<int> tmp;
std::sort(nums.begin(), nums.end());
for ( int i=; i<nums.size()-; ++i )
{
if ( i> && nums[i]==nums[i-] ) continue;
for ( int j=i+; j<nums.size()-; ++j )
{
if ( j>i+ && nums[j]==nums[j-]) continue;
int begin = j+;
int end = nums.size()-;
while ( begin<end )
{
int value = nums[i]+nums[j]+nums[begin]+nums[end];
if ( value<target )
{
begin++;
}
else if ( value>target )
{
end--;
}
else
{
tmp.push_back(nums[i]);
tmp.push_back(nums[j]);
tmp.push_back(nums[begin]);
tmp.push_back(nums[end]);
ret.push_back(tmp);
tmp.clear();
begin++;
while ( begin<end && nums[begin]==nums[begin-] ) begin++;
end--;
while ( begin<end && nums[end]==nums[end+] ) end--;
}
}
}
}
return ret;
}
};
tips:
还是学习的这个blog的思路:http://www.cnblogs.com/tenosdoit/p/3649607.html
1. 先固定一个元素i
2. 再从i+1往后遍历,每次固定一个元素j
3. 固定完j之后,就可以变成了两边夹逼的问题了。
这种思路是我见过思路最清晰的。
【4Sum】cpp的更多相关文章
- 【Permutations】cpp
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- 【Subsets】cpp
题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...
- 【Anagrams】 cpp
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- 蓝桥杯 【dp?】.cpp
题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...
- 【Triangle 】cpp
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- 【N-Queens】cpp
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 【Candy】cpp
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- 【3Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
随机推荐
- Android stutdio2.2 启动模拟器出现“/dev/kvm is not found.”解决方法
第一次启动avd,Android stutdio会自动安装Intel HAXM,而且表面看是成功的,再次启动会出现“/dev/kvm is not found.”,这说明Intel HAXM没有安装成 ...
- Cocos2d-x v3.1 安装图文教程(二)
Cocos2d-x v3.1 安装图文教程(二) 如果我们需要在Android平台上运行就必须安装android的SDK,如果我们只想在window上运行就只需要安装Cocos2d-x就行了.当 ...
- Linux让Apache支持中文URL图片/文件名
需要用到iconv_hook和mod_encoding Apache(32位): 安装环境:CentOS 5.6 + Apache 2.2.15 (Apache2.4同样适用) 安装结果:安装后支持“ ...
- linux 命令——41 ps(转)
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- MySQL设计规范与性能优化
引言 MySQL是目前使用最为广泛的关系型数据库之一,如果使用得当,可支撑企业级高并发.高可靠服务,使用不当甚至连并发量略高的个人网站都难以支撑: 就算使用了缓存,大量的数据库访问依旧在所难免,即使设 ...
- hdu-3549 Flow Problem---最大流模板题(dinic算法模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...
- Android(java)学习笔记80:Html嵌入到Java显示乱码
1. Html嵌入到Java显示乱码: 解决方案: 使用 loadData方法是中文部分会出现乱码,即使指定“utf-8”.“gbk”.“gb2312”也一样. webView.getSettings ...
- 19课 Vue第二节
事件修饰符 stop 禁止冒泡once 单次事件prevent 阻止默认事件native 原生事件(组件)keycode|name 筛选按键 组合键 : @keydown.ctrl.enter s ...
- [转] 防止js全局变量污染方法总结
javaScript 可以随意定义保存所有应用资源的全局变量.但全局变量可以削弱程序灵活性,增大了模块之间的耦合性.在多人协作时,如果定义过多的全局变量 有可能造成全局变量冲突,也就是全局变量污染问题 ...
- 拷贝时间测试=cudamelloc+cudahostalloc
/* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and its lic ...