题意

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.

给定一个数组,找出其中的四个数,使它们的和等于某个特定的数

解法

和2Sum以及3Sum一样,都是先排序然后遍历前面几个数,剩下的两个数用Two Pointers来找,能降低一个N的复杂度,这里复杂度为O(N^3)

这里采用了一种比3Sum这题里更简洁的判重方法,直接将选出的数用Map记录,每次选取前检查一次Map看是否存在。虽然效率有所牺牲,但是代码变得很简洁而且有高可读性,自认为有些时候这种交换是值得的。

class Solution
{
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
vector<vector<int>> rt;
map<vector<int>,bool> map; sort(nums.begin(),nums.end()); for(int i = 0;i < nums.size();i ++)
for(int j = i + 1;j < nums.size();j ++)
{
int k = j + 1;
int l = nums.size() - 1;
while(k < l)
{
if(nums[i] + nums[j] + nums[k] + nums[l] == target)
{
vector<int> temp = {nums[i],nums[j],nums[k],nums[l]};
if(map.find(temp) == map.end())
{
map[temp] = true;
rt.push_back(temp);
}
k ++;
} else if(nums[i] + nums[j] + nums[k] + nums[l] < target)
k ++;
else
l --;
}
}
return rt; }
};

LeetCode 4Sum (Two pointers)的更多相关文章

  1. LeetCode——4Sum &amp; 总结

    LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...

  2. [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 ...

  3. [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 ...

  4. leetcode — 4sum

    import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...

  5. Leetcode 8 Two Pointers

    Two Pointers 1. 28. Implement strStr() 用 i 记录haystack偏移量,j 记录 needle 的偏移量. class Solution { public i ...

  6. LeetCode 4Sum 4个数之和

    题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...

  7. [LeetCode] 4Sum hash表

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  8. 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 ...

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

随机推荐

  1. Prometheus Node_exporter 之 Network Sockstat

    Network Sockstat proc/net/sockstat 1. Sockstat TCP type: GraphUnit: shortLabel: SocketsTCP_alloc - 已 ...

  2. Linux开启和关闭防火墙

    一.即时生效,重启后失效: 1.启动:service iptables start 2.关闭:service iptables stop 二.重启后永久生效: 1.启动:chkconfig iptab ...

  3. Questions about UIUC and USC

    Questions about UIUC and USC I am admitted to University of Illinois at Urbana-Champaign (UIUC) Prof ...

  4. 【转】Mysql学习---SQL的优化

    [原文]https://www.toutiao.com/i6594314336913588743/ mysql如何处理亿级数据,第一个阶段--优化SQL语句 1.应尽量避免在 where 子句中使用! ...

  5. 阿里八八Alpha阶段Scrum(2/12)

    今日进度 叶文滔: 11.1:搭建Andriod Studio开发环境 11.2:已经完成Alpha阶段的APP整体框架搭建. 11.3:根据会议讨论内容,增加了模块标题栏返回键. 王国超: 完成了多 ...

  6. [python]如何理解uiautomator里面的 right,left,up,down 及使用场景

    关于Android自动化uiautomator 框架,前面有讲在有些场景下,比如需要在设置界面中将某些选项开关打开或者关闭(前提是这些选项和开关的控件(resourceId,className,tex ...

  7. Redis系列四:redis支持的数据类型

    一.字符串<String> 1. 字符串类型:实际上可以是字符串(包括XML JSON),还有数字(整形 浮点数),二进制(图片 音频 视频),最大不能超过512MB 2. 设值命令: s ...

  8. Centos7 Nginx 开机启动

    Centos 系统服务脚本目录: 用户(user) 用户登录后才能运行的程序,存在用户(user) /usr/lib/systemd/ 系统(system) 如需要开机没有登陆情况下就能运行的程序,存 ...

  9. 2018-2019-2 20165302 Exp5 MSF基础应用

    1.实验目的 掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路 2.实验内容 一个主动攻击实践; (1分) MS17-010 一个针对浏览器的攻击:(1分) ms14_064 一个 ...

  10. Kafka设计解析(二十一)关于Kafka幂等producer的讨论

    转载自 huxihx,原文链接 关于Kafka幂等producer的讨论 众所周知,Kafka 0.11.0.0版本正式支持精确一次处理语义(exactly once semantics,下称EOS) ...