题目

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: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

  1. solution set is:

[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

思路

这道题目和解决3sum的思路是一样的
思路是这样的,首先我们先不考虑去除重复的部分,把代码写出来,就很简单的用for循环,当判断最里面的两个数的时候用while循环.之后写出来了,在考虑怎么才能避免重复,就是跳过已经用过的数字就行了.

代码

var fourSum = function(nums, target) {let len = nums.length;if (nums.length < 4)return [];nums.sort(function(a, b){return a-b;});var res = [];for (let i = 0; i < len-3; i++) {if (i > 0 && nums[i] == nums[i-1]) continue;for (let j = i+1; j < len-2; j++) {if (j > i + 1 && nums[j] == nums[j-1])continue;let lo = j+1, hi = len-1;let temp;while(lo < hi) {let sum = nums[i] + nums[j] + nums[lo] + nums[hi];if (sum == target) {res.push([nums[i], nums[j], nums[lo], nums[hi]]);temp = nums[lo];while (lo < hi && temp == nums[lo])lo++;temp = nums[hi];while (lo < hi && temp == nums[hi])hi--;}else if (sum < target) {lo++;}else {hi--;}   }   }}return res;
};

4SUM Leetcode相关推荐

  1. 4Sum -- LeetCode

    原题链接:  http://oj.leetcode.com/problems/4sum/   这道题要求跟 3Sum 差不多,只是需求扩展到四个的数字的和了.我们还是可以按照 3Sum 中的解法,只是 ...

  2. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

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

  4. LeetCode算法入门- 4Sum -day11

    LeetCode算法入门- 4Sum -day11 Given an array nums of n integers and an integer target, are there element ...

  5. LeetCode 2.1.10 4Sum

    2.1.10 4Sum 描述 Given an arraySofnintegers, are there elementsa, b, c, andd inSsuch thata+b+c+d=targe ...

  6. LeetCode 454 4Sum II(哈希法)

    问题:给出四个数组A,B,C,D,问有多少组(i,j,k,l)满足A[i]+B[j]+C[k]+D[l]=0 思路:如果直接数据遍历,时间复杂度为O(n^4).可以考虑降维.先计算A,B遍历求对应和与 ...

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

  8. leetcode 18 -- 4Sum

    4Sum 题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  9. leetcode 454. 4Sum II | 454. 四数相加 II(Java)

    题目 https://leetcode.com/problems/4sum-ii/ 题解 四数相加问题. 对于左边两个数两两组合,需要维护一个 map,用来存左边两个数的 sum 以及该 sum 对应 ...

  10. LeetCode 454. 4Sum II

    454. 4Sum II Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) the ...

最新文章

  1. python用jieba进行分词并可视化
  2. iis7安装mysql_windows server 2008/2012安装php+iis7+mysql环境搭建
  3. Java程序员必看!2021最新爱奇艺Java社招面试题目
  4. 代码扫描工具测试覆盖率工具
  5. CentOs中iptables配置允许mysql远程访问
  6. 电脑用linux命令大全,电脑操作时常用的一些Linux命令
  7. Java中泛型Class《T》、T与Class《?》
  8. TensorRT加速ENet
  9. 数据结构(三)树、二叉树、最优二叉树
  10. php进度条,php 如何做进度条
  11. 蓝桥杯C语言---基础练习
  12. 封校大学生无聊玩起图像大找茬——游戏脚本(一起领略Python脚本的风采吧)
  13. 大数据-经典案例统计求和
  14. 团队协作工具,如何加强团队协作能力
  15. 网络隔离下的几种数据交换技术比较
  16. 声纹识别(说话人识别)技术
  17. 光纤中的多种光学模式芯径_光纤的结构是什么?种类有哪些?该怎么选择?
  18. SQL 校验身份证号格式
  19. 刷新页面,js实现文章浏览量自动更新
  20. java基础之数据类型

热门文章

  1. 自定义优先队列的元素权重
  2. python中eval函数和str函数
  3. 线程Java的两种方式_[转载]Java线程的两种实现方式
  4. gis投影中未定义的地理转换_PhotoScan中经纬度转换为投影坐标快捷方法
  5. 华为鸿蒙巴龙麒麟,华为5G新机强势曝光:麒麟985+巴龙5000+鸿蒙系统,颜值性能炸裂...
  6. NoSQLAttack针对 mongoDB 的攻击工具
  7. 实现类去实现接口的时候,将接口的注释一起重写出来设置
  8. Linux拷贝分区内容,dd复制分区后目标分区的大小变成原分区了
  9. 查询mysql当前连接并删除_查看mysql已有用户并删除
  10. 第2.2节 Python的语句