Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + bc + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

  1. Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
  2. A solution set is:
  3. [
  4. [-1, 0, 0, 1],
  5. [-2, -1, 1, 2],
  6. [-2, 0, 0, 2]
  7. ]
  1. public List<List<Integer>> fourSum(int[] nums, int target) {
  2. List<List<Integer>> res = new ArrayList<>();
  3. Arrays.sort(nums);
  4. for (int i = 0; i < nums.length - 3; i++) {
  5. if (i != 0 && nums[i] == nums[i - 1])
  6. continue;
  7. for (int j = i + 1; j < nums.length - 2; j++) {
  8. if (j > i + 1 && nums[j] == nums[j - 1])
  9. continue;
  10. int k = j + 1;
  11. int l = nums.length - 1;
  12. while (k < l) {
  13. int sum = nums[i] + nums[j] + nums[k] + nums[l];
  14. if (sum == target) {
  15. List<Integer> list = new ArrayList<>();
  16. list.add(nums[i]);
  17. list.add(nums[j]);
  18. list.add(nums[k]);
  19. list.add(nums[l]);
  20. res.add(list);
  21. k++;
  22. l--;
  23. // 去重复
  24. while (k < l && nums[k] == nums[k - 1]) {
  25. k++;
  26. }
  27. while (k < l && nums[l] == nums[l + 1]) {
  28. l--;
  29. }
  30. } else if (sum < target) {
  31. k++;
  32. } else {
  33. l--;
  34. }
  35. }
  36. }
  37. }
  38. return res;
  39. }
  1. public List<List<Integer>> fourSum3(int[] num, int target) {
  2. ArrayList<List<Integer>> ans = new ArrayList<>();
  3. if (num.length < 4)
  4. return ans;
  5. Arrays.sort(num);
  6. for (int i = 0; i < num.length - 3; i++) {
  7. if (num[i] + num[i + 1] + num[i + 2] + num[i + 3] > target)
  8. break; // first candidate too large, search finished
  9. if (num[i] + num[num.length - 1] + num[num.length - 2] + num[num.length - 3] < target)
  10. continue; // first candidate too small
  11. if (i > 0 && num[i] == num[i - 1])
  12. continue; // prevents duplicate result in ans list
  13. for (int j = i + 1; j < num.length - 2; j++) {
  14. if (num[i] + num[j] + num[j + 1] + num[j + 2] > target)
  15. break; // second candidate too large
  16. if (num[i] + num[j] + num[num.length - 1] + num[num.length - 2] < target)
  17. continue; // second candidate too small
  18. if (j > i + 1 && num[j] == num[j - 1])
  19. continue; // prevents duplicate results in ans list
  20. int low = j + 1, high = num.length - 1;
  21. while (low < high) {
  22. int sum = num[i] + num[j] + num[low] + num[high];
  23. if (sum == target) {
  24. ans.add(Arrays.asList(num[i], num[j], num[low], num[high]));
  25. while (low < high && num[low] == num[low + 1])
  26. low++; // skipping over duplicate on low
  27. while (low < high && num[high] == num[high - 1])
  28. high--; // skipping over duplicate on high
  29. low++;
  30. high--;
  31. }
  32. // move window
  33. else if (sum < target)
  34. low++;
  35. else
  36. high--;
  37. }
  38. }
  39. }
  40. return ans;
  41. }

LeetCode_18 4Sum的更多相关文章

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

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

  3. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  4. 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum

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

  5. No.018:4Sum

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

  6. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  7. 3Sum & 4Sum

    3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...

  8. 【leetcode】4Sum

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

  9. 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum

    2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...

随机推荐

  1. 【bzoj4412】[Usaco2016 Feb]Circular Barn

    先看成一条链 for一遍找位置 在for一遍算答案 #include<algorithm> #include<iostream> #include<cstring> ...

  2. 2.eclipse 插件安装烦死人(2)

    安装插件的实际结果是:(烦死人),要不是很多插件找不到,要不就是版本不对,要不就是下载了装上没有效果,要不就是在线安装(速度爆慢),好不容易等到结果了,结果是些错…… 最后我的eclipse 3.5. ...

  3. 步长为float

    import numpy as np for i in np.arange(0.005, 0.05, 1): print(i)

  4. H264--2--语法及结构[5]

    名词解释 场和帧 :    视频的一场或一帧可用来产生一个编码图像.在电视中,为减少大面积闪烁现象,把一帧分成两个隔行的场. 片:             每个图象中,若干宏块被排列成片的形式.片分为 ...

  5. CSU 1807: 最长上升子序列~ 分类讨论

    1807: 最长上升子序列~ Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 17[Submit][Status][Web Bo ...

  6. Opencv+Zbar二维码识别(二维码校正)

    二维码和车牌识别基本都会涉及到图像的校正,主要是形变和倾斜角度的校正,一种二维码的畸变如下图: 这个码用微信扫了一下,识别不出来,但是用Zbar还是可以准确识别的~~. 这里介绍一种二维码校正方法,通 ...

  7. ZOJ3962 2017 E.Seven Segment Display

    数码管从某个状态顺序转移N个状态 计算总共有多少个数码管被点亮 N<=10^9 观察数码管的变化规律,有明显的周期和重复,利用这个性质,计算相对于初始状态,某一位上的某个状态重复了多少次,就可以 ...

  8. Hadoop伪分布式模式搭建

    title: Hadoop伪分布式模式搭建 Quitters never win and winners never quit. 运行环境: Ubuntu18.10-server版镜像:ubuntu- ...

  9. Linux下磁盘分区、挂载、卸载操作记录

    Linux下磁盘分区.挂载.卸载操作记录. 操作环境:CentOS release 6.5 (Final) Last :: from 118.230.194.76 [root@CentOS ~]# [ ...

  10. 一个完整的mybatis项目,包含增删改查

    1.导入jar包,导入相关配置文件,均在自己博客园的文件中 编写mybatis.xml文件 <?xml version="1.0" encoding="UTF-8& ...