题目等级: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. Python3-Set

    # Set(集合) # 集合(set)是一个无序不重复元素的序列. # 基本功能是进行成员关系测试和删除重复元素. # 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须 ...

  2. shareSDK集成遇到的问题汇总

    问题一.平台添加应用时需要输入的签名(下图)的获取方式 第一步.通过android studio生成签名保存在本地 第二步.查看签名的信息 问题二.由于同事集成了QQ登录和微信登录,后来又需要加入微博 ...

  3. [CCTF] pwn350

    0x00: 之前打了CCTF,在CCTF的过程中遇到一个比较有意思的思路,记录一下. 0x01: 可以看到,这是一个 fmt 的漏洞,不过很简单,接收的输入都在stack中,可以确定输入在栈中的位置, ...

  4. ThinkPHP系统常量

    _ROOT__ : 网站根目录地址 __APP__ : 当前项目(入口文件)地址 __URL__ : 当前模块地址 __ACTION__ : 当前操作地址 __SELF__ : 当前 URL 地址 _ ...

  5. 解决xshell无法连接virtualbox中的虚拟机(Ubuntu18.04)的问题

    遇到这个问题第一反应是是否安装相应的组件: sudo apt-get install openssh-server 开启防火墙端口 firewall-cmd --zone=/tcp --permane ...

  6. CUDA-F-2-0-CUDA编程模型概述1

    Abstract: 本文介绍CUDA编程模型的简要结构,包括写一个简单的可执行的CUDA程序,一个正确的CUDA核函数,以及相应的调整设置内存,线程来正确的运行程序. Keywords: CUDA编程 ...

  7. Fire Game (FZU 2150)(BFS)

    题解:一开始想错了,以为只要烧完就是那个答案,但是这不是最优的结果,需要每两个点都bfs一遍,找到如果能够全部烧完,找到花费时间最小的,如果不能return -1.在bfs的时候,记录答案的方法参考了 ...

  8. $\LaTeX$数学公式大全3

    $3\ Delimiters$$|$ |$\vert$ \vert$\|$ \|$\Vert$ \Vert$\{$ \{$\}$ \}$\langle$ \langle$\rangle$ \rangl ...

  9. 网络爬虫技术实现java依赖库整理输出

    网络爬虫技术实现java依赖库整理输出 目录 1       简介... 2 1.1      背景介绍... 2 1.2      现有方法优缺点对比... 2 2       实现方法... 2 ...

  10. 04.重建二叉树 (Java)

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...