【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 ...
随机推荐
- mathtype 章节号 Equation Chapter 1 Section 1 的去除
mathtype 章节号 Equation Chapter 1 Section 1 的去除 转:http://hi.baidu.com/17ximm/blog/item/2882413e92fc96c ...
- [转]latex符号
常用数学符号的 LaTeX 表示方法 (以下内容主要摘自“一份不太简短的 LATEX2e 介绍”) 1.指数和下标可以用^和_后加相应字符来实现.比如: 2.平方根(square root)的输入命令 ...
- Android 在已有工程中实现微信图片压缩
这个我们需要自己去编译,但是已经有人帮我们编译好了,压缩算法也已经实现,因此,我们去下载然后编译即可:https://github.com/bither/bither-android-lib 首先将上 ...
- 【c++】用c++编写的求任意区间的素数的小程序
#include using namespace std; int main() { cout<<"*************************************** ...
- File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录)
File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录) 以前的两篇文章(收录在书本「上集」的第十七章) 请看「ASP.NET专题实务」,松岗出版 File 与 Log ...
- python时间转换 ticks-FYI
#设a为字符串 import time a = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组 time.strptime(a,'%Y-% ...
- Aizu 0525 Osenbei(状压+贪心)
题意:翻煎饼,只能横着翻或者竖着翻.问最多有多少朝上? 行只有10,所以枚举一下2^10的状态,每列取0或1中最大的一个. 在枚举外面把饼翻好,枚举里面指针指一下就好.(位运算或bitset乱搞 #i ...
- Android(java)学习笔记96:layout_weight使用注意事项
1. android:layout_weight使用说明: layout_weight是权重的意思,也就是各个控件所占的比重,用在LinearLayout布局中.当我们使用layout_weight的 ...
- JavaScript操作Array对象常用的方法
转换方法 因为JavaScript内部机制(继承),所有的对象都具有toLocalString() .toString().valueOf()方法,Array也不例外so:var colors = ...
- mongdb 一些操作
一.命令操作数据库1.管理员身份打开cmd2.进到mongdb的mongo.exe文件所在路径3.show dbs 查看mongodb4.连接远程数据库:mongo ip:端口/数据库5.打开某个数据 ...