题目

Given an array S of n integers, are there elements abc, 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的更多相关文章

  1. 【Permutations】cpp

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  2. 【Subsets】cpp

    题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...

  3. 【Anagrams】 cpp

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  4. 蓝桥杯 【dp?】.cpp

    题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...

  5. 【Triangle 】cpp

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  6. 【N-Queens】cpp

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  7. 【Combinations】cpp

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  8. 【Candy】cpp

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  9. 【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 ...

随机推荐

  1. C++中构造函数的写法

    class Circle { public: Circle(float r); private: float radius; }; Circle::Circle(float r) { radius = ...

  2. uvm_dpi——DPI在UVM中的实现(一)

    文件: src/dpi/uvm_dpi.svh 类:  无   SystemVerilog DPI,全称SystemVerilog直接编程接口 (英语:SystemVerilog Direct Pro ...

  3. ASP.NET的三种开发模式

    前言 ASP.NET 是一个免费的Web开发框架,是由微软在.NET Framework框架中所提供的,或者说ASP.NET是开发Web应用程序的类库,封装在System.Web.dll 文件中.AS ...

  4. java Vamei快速教程18 容器

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java中有一些对象被称为容器(container).容器中可以包含多个对象,每个 ...

  5. <已解决>使用selector设置Button按下松开的样式以及 <item> tag requires a 'drawable' attribute or child tag defining a drawable 报错

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="ht ...

  6. IOS 创建一个可以随意拉伸不变形的图片

    创建一个扩展 UIImage的类 #import "UIImage_Extension.h" @implementation UIImage+Extension /** *返回一张 ...

  7. World Wind Java开发之七——读取本地栅格文件(影像+高程)构建三维场景(转)

    http://blog.csdn.net/giser_whu/article/details/41679515 首先,看下本篇博客要达到的效果图: 下面逐步分析如何加载影像及高程文件. 1.World ...

  8. 【洛谷3232】[HNOI2013] 游走(贪心+高斯消元)

    点此看题面 大致题意: 一个无向连通图,小\(Z\)从\(1\)号顶点出发,每次随机选择某条边走到下一个顶点,并将\(ans\)加上这条边的编号,走到\(N\)号顶点时结束.请你对边进行编号,使总分期 ...

  9. 索引属性 unique指定

    比较重要的属性有: 名字 db.collection.ensureIndex({},{name:''}) 在创建索引时,mongodb会自己给索引创建默认的名字,这种名字并不好记,我们看一下mongo ...

  10. OO终章

    一,第四单元架构设计 第一次作业:只有类图 1,重置MyClass,MyOperation等类,为使里面只有必要数据(name,id,visibility等).或方便组织数据(如MyClass作为其底 ...