题意

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. 报错:java.net.bindexception: address already in use: jvm_bind:8080

    原因:8080端口被占用 这说明80端口(该端口是Tomcat的监听端口)已经被其他程序占用,先用命令提示符 " netstat -ano " 命令显示端口状态,再在结果中找到端口 ...

  2. 数据库导入.bacpac 文件创建新实例

    先连接好数据库,然后打开左侧 对象资源管理器,选择数据库  右键单击 ---> 选择导入数据层应用程序 根据提示向导一步步走就行了 部分导入失败以及处理方案 异常1 : 在数据库master中拒 ...

  3. IP地址的分类——a,b,c 类是如何划分的【转】

    ip分类已经是耳熟能详了.但是说的都比较繁琐,这里简述一下,便于以后复习. IP地址,一共分成了5类,范围分别如下: A类IP:从0.0.0.0 – 127.255.255.255,共有1677721 ...

  4. linux正则表达式(基础正则表达式+扩展正则表达式)

    正则表达式应用非常广泛,例如:php,Python,java等,但在linux中最常用的正则表达式的命令就是grep(egrep),sed,awk等,换句话 说linux三剑客要想能工作的更高效,就一 ...

  5. Hadoop HBase概念学习系列之HRegion服务器(三)

    所有的数据库数据一般是保存在Hadoop分布式系统上面的,用户通过一系列HRegion服务器获取这些数据.一台机器上一般只运行一个HRegion服务器,而且每一分区段的HRegion也只会被一个HRe ...

  6. UltraISO制作使用(服务器装机u盘制作)

    1.准备工作: 1)U盘一个,需要格式化(大于4G,毕竟ISO文件就已经大于4G了) 2)CentOS7.1 iso文件一个(去这里下载:http://www.centoscn.com/) 3)Ult ...

  7. Django基础与组件

    第一章:Django系列之web应用与http协议 第二章:基于wsgiref模块DIY一个web框架 第三章:Django下载与简介 第四章:url控制系统 第五章:视图 第六章:Django模板语 ...

  8. React-Native 真机调试踩坑指南

    继上一篇基础安装踩坑继续我们的踩坑之旅,备注一下以下仅针对Mac环境-- 安卓 1.adb 找不到命令? Adb的全称为Android Debug Bridge,就是起到调试桥的作用,真机调试安卓必备 ...

  9. Linux chmod +755和chmod +777 各是什么意思呢?

    你可以在linux终端先输入ls -al,可以看到如: -rwx-r--r-- (一共10个参数)第一个跟参数跟chmod无关,先不管.2-4参数:属于user5-7参数:属于group8-10参数: ...

  10. MP实战系列(十五)之执行分析插件

    SQL 执行分析拦截器[ 目前只支持 MYSQL-5.6.3 以上版本 ],作用是分析 处理 DELETE UPDATE 语句, 防止小白或者恶意 delete update 全表操作! 这里我引用M ...