题目等级:4Sum(Medium)

题目描述:

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums 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.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

  题意:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。


解题思路:

  这个就没什么特别的了,直接参考三数之和:【LeetCode】15、三数之和为0

  网上也没有找到什么其他特别的解法,有说采用二分的方法转化为两个两数之和的,但是感觉过于繁琐了,不太直观,这里就直接采用在三数之和的外层又加了一层循环,时间复杂度为:O(n^3).

  然后主要对这类题做一个总结:

  两数之和系列,做法:

  三数之和(N数之和)序列,做法:

  • 第一步:java.util.Arrays.sort(int[] nums),升序排列
  • 第二步:N-2层for循环,外加两个双向指针,双向指针后需while循环向数组中间靠拢,while循环里嵌套两层while循环,用于去重
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res=new ArrayList<>();
if(nums==null && nums.length==0)
return res;
Arrays.sort(nums);
int len=nums.length;
for(int i=0;i<len-3;i++){
if(i>0 && nums[i-1]==nums[i]) //跳过重复的
continue;
for(int j=i+1;j<len-2;j++){
if(j>i+1 && nums[j]==nums[j-1]) //跳过重复的
continue;
int low=j+1,high=len-1,sum=target-nums[i]-nums[j];
while(low<high){
if(nums[low]+nums[high]==sum){ //找到一个解
res.add(Arrays.asList(nums[i],nums[j],nums[low],nums[high]));
while(low<high && nums[low+1]==nums[low])
low++;
while(low<high && nums[high-1]==nums[high])
high--;
low++;
high--;
}else if(nums[low]+nums[high]<sum)
low++;
else
high--;
}
}
}
return res;
}
}

【LeetCode】18、四数之和的更多相关文章

  1. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

  2. LeetCode 18. 四数之和(4Sum)

    题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...

  3. [Leetcode 18]四数之和 4 Sum

    [题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...

  4. [LeetCode] 18. 四数之和

    题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...

  5. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  6. 【LeetCode】18.四数之和

    题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...

  7. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  8. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

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

  10. 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

随机推荐

  1. .Net界面开发神器—DevExpress官方汉化包免费下载!还在等什么?

    点击获取DevExpress v19.1.7新版试用下载 DevExpress Localization Service允许您创建一组自定义的附属程序集,要将语言包添加到程序集中,请查看本文中为大家列 ...

  2. 水果商城 ( Iview+ SSM + MySQL )

    因为时间原因,只做了后台,前台本来是打算使用 uni 框架 的. 有文档.E-R流程图.数据库文件. 项目源码地址:https://github.com/oukele/MyProject-Two

  3. spring-boot + mybatis + mysql+shiro 整合

    参考地址 https://blog.csdn.net/clj198606061111/article/details/82948757 https://blog.csdn.net/ityouknow/ ...

  4. golang配置oci8所遇到问题解决

    新建文件夹 mingw 将 MinGW.zip 解压到mingw目录下,进入mingw\lib目录下 新建文件夹pkg-config 执行命令 go get github.com/wendal/go- ...

  5. Flutter 父子组件传值

    Flutter 父子组件传值 一父传子: 父中: void onButtonChange(val1,val2,val3){ print('============================子向父 ...

  6. Apache+php搭建

    首先安装Apache -->下载 修改httpd.conf文件 # # This is the main Apache HTTP server configuration file. It co ...

  7. sh_20_for语法演练

    sh_20_for语法演练 for num in [1, 2, 3]: print(num) if num == 2: break else: # 如果循环体内部使用break退出了循环 # else ...

  8. Codeforces 785 D.Anton and School - 2(组合数处理)

    Codeforces 785 D.Anton and School - 2 题目大意:从一串由"(",")"组成的字符串中,找出有多少个子序列满足:序列长度为偶 ...

  9. Android_(控件)使用ListView显示Android系统SD卡的文件列表_02

    使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局item_filelayout(一个文件的单独存放) 运行截图: 程序结构 <?x ...

  10. CSP2019游(AFO?)记

    Day 1 不知道为啥一看到\(T1\)就想到\(longlong\)可能存不下,试了下果然. \(T2\)想了半个小时胡出个\(O(n)\)算法,但是假了.冷静了一下,做了前缀和之后,合法的子区间\ ...