【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 ...
随机推荐
- JAXB介绍二
链接上一遍 JAXB介绍一 , 本节主要介绍解析xml的步骤, 下面的例子是在实际项目中运用的, 把它拿出来单独写一个java运行程序. 5. 测试实例 先给出我的代码结构图: 再给出要解析的Scri ...
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...
- VMware NAT端口映射外网访问虚拟机linux可能会出现的错误总结
博主因为做实验报告的缘故,尝试以NAT的方式从外网远程连接到虚拟机的linux操作系统:https://www.cnblogs.com/jluzhsai/p/3656760.html,本文主要举出在此 ...
- POJ 2392 Space Elevator(多重背包)
显然塔的总高度不会超过最大的a[i],而a[i]之前的可以到达的高度 是由a值更小的块组成,所以按照a从小到大的顺序去转移. 然后就是多重背包判断存在性了,几乎和coin那题一样. 数据没coin丧病 ...
- 【BZOJ4196】[NOI2015] 软件包管理器(树链剖分)
点此看题面 大致题意: 有\(n\)个软件包,它们的依赖关系形成一棵树.现在,问你安装或卸载一个软件包,会影响多少个软件包的安装状态. 树链剖分 这道题应该是 树链剖分 算法比较入门的题目吧. 对于安 ...
- CPP-基础:C_C++变量命名规则
C_C++变量命名规则 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++必须遵守的变量命名规则: 1. 变量名只能是字母(A-Z,a-z)和数字(0-9)或者下划线(_)组成. 2. 第一 ...
- js数据结构处理--------扁平化数组处理为树结构数据
将扁平化的数组处理为树结构数据,我们可以利用对象来处理,对象的复制是浅拷贝,指向相同的内存地址: var arr = [ { id: 0, pid: -1, name: 'sadas' }, { id ...
- Oracle小知识_长期总结
更新时间:2018年7月16日 11:22:28 一. 系统 1. 打开防火墙后 Oracle 无法链接 新建1521端口规则. 二.知识 A. 序列 1. nextval ------------- ...
- 如何在Git提交空文件夹
1,git clone url 拉取代码至本地 2,mkdir 文件夹名称 在本地创建文件夹 3,cd 文件夹名称 git init 初始化文件夹 vi .gitkeep 创建.gitkeep文件,内 ...
- WP Mail SMTP插件解决Contact Form 7表单提交失败问题
WP Mail SMTP插件解决Contact Form 7表单提交失败问题 WP Mail SMTP是一款非常优秀的解决WordPress主机因为不支持或者是禁用了mail()函数,导致无法实现在线 ...