首页 > 其他分享 >【LeeCode】18. 四数之和

【LeeCode】18. 四数之和

时间:2023-02-07 18:00:25浏览次数:51  
标签:四数 end nums int 18 list start LeeCode new

【题目描述】

给你一个由 ​​n​​​ 个整数组成的数组 ​​nums​​​ ,和一个目标值 ​​target​​ 。请你找出并返回满足下述全部条件且不重复的四元组 ​​[nums[a], nums[b], nums[c], nums[d]]​​ (若两个四元组元素一一对应,则认为两个四元组重复):

  • ​0 <= a, b, c, d < n​
  • ​a​​​、​​b​​​、​​c​​​ 和 ​​d​​ 互不相同
  • ​nums[a] + nums[b] + nums[c] + nums[d] == target​

你可以按 任意顺序 返回答案 。

​​​​https://leetcode.cn/problems/4sum/​


【示例】

【LeeCode】18. 四数之和_System


【代码】​​韦溜溜​

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
int i,left ,right;
long sum;
Set<String> set = new HashSet<>();
Arrays.sort(nums);
List<List<Integer>> lists = new ArrayList<>();
for (int k = 0; k < nums.length ; k++) {
for (i = k + 1; i < nums.length; i++) {
left = i+1;
right = nums.length - 1;
while (left < right){
sum = (long)nums[k] + nums[i] + nums[left] + nums[right];
if (sum == target){
List<Integer> integerList =
Arrays.asList(nums[k], nums[i], nums[left], nums[right]);
String key = integerList.toString();
if(!set.contains(key)){
set.add(key);
lists.add(integerList);
}
right--;
left++;
//找到了还得接着移动
} else if (sum > target ) {
right--;
} else {
left++;
}
}
}
}
return lists;
}
}


【代码】admin

通过率:281 / 292  需要注意的是因为第一次获取到list集合后, break退出了 需要继续判断 start++/end--

思路参考 ​​【LeeCode】16. 最接近的三数之和  ​

基于排序 + 双指针,区别的是这里是四个数字, 所以用了3层循环, i,j为基础, start和end循环遍历 

package com.company;

import java.util.*;

// 2022-02-07
class Solution {
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
int start = j + 1;
int end = nums.length - 1;
if (start > end) break;
LinkedList<Integer> list = new LinkedList<>();
int sum = 0;
while (start < end) {
// System.out.println(nums[i] + ": " + nums[j] + ": " + + nums[start] + ": " + nums[end]);
sum = nums[i] + nums[j] + nums[start] + nums[end];
if (sum == target) {
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[start]);
list.add(nums[end]);
if (!res.contains(list) && list.size() == 4) {
res.add(list);
list = new LinkedList<>();
}
// 注意: 这里是需要继续判断的,不能直接break
start++;
end--;
} else if (sum > target) {
end--;
} else {
start++;
}
}
}
}
System.out.println(res);
return res;
}
}

public class Test {
public static void main(String[] args) {
new Solution().fourSum(new int[]{1, 0, -1, 0, -2, 2}, 0); // 输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
new Solution().fourSum(new int[]{2,2,2,2,2}, 8); // 输出:[[2,2,2,2]]
}
}

第二版本: 通过率 291/292  最后2个版本超时了

package com.company;

import java.util.*;

// 2022-02-07
class Solution {
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
int start = j + 1;
int end = nums.length - 1;
if (start > end) break;
long sum = 0;
while (start < end) {
sum = nums[i] + nums[j] + nums[start] + nums[end];
if (sum == target) {
List<Integer> list = Arrays.asList(nums[i], nums[j], nums[start], nums[end]);
if (!res.contains(list)) {
res.add(list);
}
start++;
end--;
} else if (sum > target) {
end--;
} else {
start++;
}
}
}
}
System.out.println(res);
return res;
}
}

public class Test {
public static void main(String[] args) {
new Solution().fourSum(new int[]{1, 0, -1, 0, -2, 2}, 0); // 输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
new Solution().fourSum(new int[]{2,2,2,2,2}, 8); // 输出:[[2,2,2,2]]
}
}

标签:四数,end,nums,int,18,list,start,LeeCode,new
From: https://blog.51cto.com/u_13682316/6042602

相关文章

  • 训练总结 2018.11.15
    昨天第一次打线上CFdiv2,感觉自己还太嫩了,第一题,从本来读对题意,到读错题意,然后读全题意,但还是读错了,真是,把我弄到上天,比赛结束也没能A,赛后听学长指导,终于明白题意了,A了,自己还......
  • 训练日志 2018.10.11
    昨天晚上打比赛,感觉手好生,题意看了半天,才看懂,然后就是TLE,这回还好一点,马上想到了,修改的算法,但是细节没处理好,WA了,找了好一会才发现代码的错误,第二题就更艰辛了,一开始就跑偏......
  • 训练日记 2018.12.14
        哎,这几天被树形背包搞懵了,一开始感觉没学到啥,做一个题看一个题解,每个题单个来看都能看懂,但是遇到一个新题就不会了,而且你用上一个题的做法做,依旧不对,网上的题解......
  • [08001][unixODBC]zabbix 6.2 [Microsoft][ODBC Driver 18 for SQL Server]SSL Provid
    环境:Centos9stream 这个问题大致原因是,数据库证书认证失败。先说解决方法:1.首先确保openssl是1.1.1版本的,如果是3.2.0可以尝试卸载该版本或重装系统为linux Centos8str......
  • 【LeeCode】16. 最接近的三数之和
    【题目描述】给你一个长度为 ​​n​​​ 的整数数组 ​​nums​​ 和一个目标值 ​​target​​​。请你从 ​​nums​​ 中选出三个整数,使它们的和与 ​​target......
  • 如何去阅读源码,我总结了18条心法
    大家好,我是三友~~这篇文章我准备来聊一聊如何去阅读开源项目的源码。在聊如何去阅读源码之前,先来简单说一下为什么要去阅读源码,大致可分为以下几点原因:最直接的原因,就......
  • POJ 3518 Prime Gap 素数打表
    算法分析:题意:水题一枚:题意找到包含所给数的一个数列,该数列满足在两个相邻质数之间,数列包含最大的质数,举个例子,10,则7,8,9,10,11,则数列为8,9,10,11分析:直接打表欧克代码实现PrimeGap......
  • ACM-ICPC 2018 沈阳赛区现场赛 K. Let the Flames Begin (约瑟夫环问题 n个人, 报数为k
     题意: n个人围成一个圈,从1开始报到第k个人出环,问第m个出环的人是谁,n、m、k<=1e18且min(m,k)<=2e6。题解:约瑟夫环的出队是有O(n)的递推算法的:f(n)=(f(n-1)+k-1)......
  • 2019/4/18
     今天把最近打的51nod的比赛认为好题或者比赛的时候没做出来、没做到的题补了补,这些题,大部分都做过,看着眼熟,但依旧有的能出,有的不能出,害的好好下功夫,至少省赛前把三集题做......
  • 智慧工地、雪亮工程、明厨亮灶等各类视项目通过GB28181汇聚视频监控到LiveGBS流媒体管
    目前市面上各类监控设备(摄像头、录像机、监控管理平台)等基本都支持GB28181协议。当设备通过GB28181统一汇聚到LiveGBS流媒体视频平台后,LiveGBS管理页面会管理所有接入进来......