LeetCode 4Sum 4个数之和
题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target。以vector<vector<int>>来返回,也就是二维的,列长为4,有多少个序列就多少行,每行都是唯一的,且升序。
思路:

方法一:用类似3sum的方法,先确定下第1个元素,再确定第2个元素,剩下两个元素用“两个指针”。前提是已排序。这个方法主要是怎么去重,这里提供两种方法:
1)用unordered_set,只要找到一个序列就检查里面有没有这样的序列,若没有就插入,这样保证了唯一性,最后再用迭代器遍历一次,逐个搬到vector中返回。
2)这是我用的方法。假设有序列 a{-2,-2,-2,-1,-1,0,0,1,1,2,2 }共11个元素,target=0。
第1个元素取a[0],第2个元素取a[1],那么>1的所有两个数的组合会被“两个指针”所全部找到,而如果有重复的,都会是连续的重复,所以只要判断与上一个序列之中有一个值不同,就可以进行插入。(“两个指针”处的去重)
接下来第2个元素会取a[2],但是a[1]=a[2],还有必要再试吗?不必要,看{-2,-2,a,b}=target,这里a和b已经将所有可能给试了,如果这此仍取a[2],那么仍然在试集合{-2,-2,a,b}中的a和b的值而已。这下如果重复了,就不一定会连续的重复了(可以自己列出),去重就麻烦了。所以第2个元素必须跳过已经扫过的值,也就是无论还有几个-2,直接跳过到-1。(第2个元素处的去重)
第1个元素要取a[1]开始试吗?不用!道理同第2个元素的去重一样。所有以第1个元素为-2的序列已经都试出来了,再取-2也只是再找重复的序列,而且不是连续的。
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
sort(num.begin(), num.end()); //排序
vector<int> group(,);
vector< vector<int> > ans;
int n=num.size(), sum, sum2, *pl, *pr, old1=,old2=- ;//这里的old可以随便取值,特殊一点的都行
for(int j=; j<n-; j++ ) //第1个元素
{
if(old1==num[j]) continue; else old1=num[j];
for(int i=j+; i<n-; i++ )//第2个元素
{
if(old2==num[i]) continue; else old2=num[i];
sum2 = target-num[i]-num[j] ;//寻找余下两数之和
pl = &num[i+];//左指针
pr = &num[n-];//右指针
while(pl!=pr)
{
sum = *pl + *pr;
if( sum == sum2 )
{
if( group[]!=num[j] || group[]!=num[i] || group[]!=*pl || group[]!=*pr )//只要有一个不同,便可添加
{
group[] = num[j];
group[] = num[i];
group[] = *pl;
group[] = *pr;
ans.push_back(group);
}
pl++;
}
else if( sum > sum2 ) pr--;
else pl++;
}
}
old2=;
}
return ans;
}
};
4Sum
方法二:将序列中两两的和作为新的序列,那问题就转化为“求两个值的和”,也就是用“两个指针”法。这里的难点在于,找到和为target的新序列中的两个元素,如何找到他们原来的面目(4个元素)。还有个问题,元素会被你重复的利用。
比如,有序列a{-1,-1,0,1,1},
转成新序列{-2,-1,0,0,-1,0,0,1,,2},
新序列是这么来的{-1+-1,-1+0,-1+1,-1+1,-1+0,-1+1,-1+1,0+1,0+1,1+1},
也就是{a[0]+a[1],a[0]+a[2], a[0]+a[3],a[0]+a[4] ,a[1]+a[2],a[1]+a[3],a[1]+a[4],a[2]+a[3],a[2]+a[4],a[3]+a[4]}。
假如target=0,我们在新序列中找到 -1+1=0,但是他们是a[0]+a[2]+a[2]+a[4]。也就是说,a[2]被算了两次了,是不允许的。
此方法还未实现,有空再想。
LeetCode 4Sum 4个数之和的更多相关文章
- [LeetCode] 4Sum 四数之和
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- LeetCode 16 3Sum Closest (最接近target的3个数之和)
题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description Problem : 找到给定数组中a+b+c 最接近targe ...
- [LeetCode] 18. 4Sum 四数之和
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 18 4Sum(寻找四个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode——4Sum & 总结
LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...
- LeetCode:三数之和【15】
LeetCode:三数之和[15] 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的 ...
随机推荐
- struts2知识点汇总
标签遍历Map<key,List<Object>> listMap=new HashMap<String,List<Student>>(); List& ...
- Codeforces Round #522 Div2C(思维)
#include<bits/stdc++.h>using namespace std;int a[200007];int b[200007][7];int ans[200007];int ...
- AOP常用注解
1.@Aspect 配置切面Bean,和<bean.../>元素进行配置无区别,一样支持依赖注入来配置属性值: 如果启动了Spring的"零配置"特性,一样可以让Spr ...
- centos禁止ping
1.修改配置文件/etc/sysctl.conf 在这个文件的最后添加一行: net.ipv4.icmp_echo_ignore_all=1 (0 代表允许 1 代表禁止) 2.执行sysctl -p ...
- Oracle存储过程实例分析总结(代码)
1.存储过程结构 1.1 第一个存储过程 ? 1 2 3 4 5 6 7 8 9 10 11 12 CREATE OR REPLACE PROCEDURE proc1 ( para1 varc ...
- jQuery中ready和load的区别
<span style="white-space:pre"> </span>//document ready $(document).read ...
- DB2的常用数据类型
今天在db2中查询数据时出现错误: Overflow occurred during numeric data type conversion:SQLCODE=-413, SQLSTATE=22003 ...
- java——helloword
第一次接触Java,感觉乱乱的,需要捋清楚一些概念再安装java. 首先,什么是JDK,什么是JRE呢? JRE:JAVA运行环境(Java Runtime Envirnment) JDK:Java开 ...
- poj(2406) kmp
题目链接:https://vjudge.net/problem/POJ-2406 kmp学习:https://blog.csdn.net/starstar1992/article/details/54 ...
- UVALive 7511 L - Multiplication Table 数学模拟题,暴力
给定一副表,问其是否合法. 思路:当全部是?的时候,是合法的. 如果不是,那么,就找到一个数字,把它拆成若干个a*b的形式,去判断其它点是否合法即可. 拆分数字的时候,只需要枚举到sqrt(n),因为 ...