18. 四数之和

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

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:

[

[-1, 0, 0, 1],

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

[-2, 0, 0, 2]

]

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/4sum

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
int len = nums.length;
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(nums.length<4) return res;
Arrays.sort(nums);
if(nums[0]>target/4 || nums[len-1]<target/4) return res;
for(int i = 0;i<len;i++){
if(nums[i]>target/4) break;
if(i>0 && nums[i]==nums[i-1]) continue;
int sum = target-nums[i];
for(int j = i+1;j<len;j++){
if(nums[j]>sum/3) break;
if(j>i+1 && nums[j]==nums[j-1]) continue;
int l = j+1;
int r = len-1;
while(l<r){
if(nums[r]<sum/3) break;
int temp = nums[j] + nums[l] +nums[r];
if(temp == sum){
res.add(Arrays.asList(nums[i],nums[j],nums[l],nums[r]));
while(l<r && nums[l] == nums[l+1]) l++;
while(l<r && nums[r] == nums[r-1]) r--;
l++;
r--;
}else if(temp>sum){//结果大了右指针往左
r--;
}else{//结果小了左指针往右
l++;
}
}
}
}
return res;
}
}

Java实现 LeetCode 18 四数之和的更多相关文章

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

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

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

  3. [LeetCode] 18. 四数之和

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

  4. LeetCode:四数之和【18】

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

  5. 【LeetCode】18.四数之和

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

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

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

  7. 【LeetCode】四数之和

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

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

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

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

随机推荐

  1. 常见的GAN网络的相关原理及推导

    常见的GAN网络的相关原理及推导 在上一篇中我们给大家介绍了GAN的相关原理和推导,GAN是VAE的后一半,再加上一个鉴别网络.这样而导致了完全不同的训练方式. GAN,生成对抗网络,主要有两部分构成 ...

  2. DP之石子堆合并问题

    相邻 环形 总结 (1)相邻:在一个圆形操场的四周摆放着n堆石子(n<= 100),现要将石子有次序地合并成一堆.规定每次只能选取相邻的两堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得 ...

  3. 【hdu1030】“坐标表示法”

    http://acm.hdu.edu.cn/showproblem.php?pid=1030 算法:以顶点为原点,建立坐标系,一个数可以唯一对应一个三元组(x, y, z),从任意一个点出发走一步,刚 ...

  4. SpringData:关联查询

    一.查询方式 1.导航式查询 使用“对象.属性”   进行查询:对于多的查询, 默认就是延迟加载,添加注解@Transactional 在OneToMany 注解中需要添加属性   fetch:值:F ...

  5. WIn7系统下配置Java环境变量

    给个官网下载地址  :https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.首先 ...

  6. 编译nginx的时候报错 需要安装PCRE

    ./configure --prefix=/mynginx/ 本地编译nginx的时候 报错 提示需要安装PCRE 错误信息: ./configure: error: the HTTP rewrite ...

  7. spring mvc --自定义converse

    在MVC中我们可以很轻松的根据项目需求进行必要的信息转换,如设置默认的日期格式,自定义String类型的格式等等... 配置中我们需要自定义converseService: <bean id=& ...

  8. search(14)- elastic4s-统计范围:global, filter,post-filter bucket

    聚合一般作用在query范围内.不带query的aggregation请求实际上是在match_all{}查询范围内进行统计的: GET /cartxns/_search { "aggs&q ...

  9. ql的python学习之路-day3

    字典操作 特性: 1.无序的 2.key是唯一的 , ,,], ,,], ,,], }, ,,], 'bbb' : ['a', 'b', 'c'], }}

  10. ReactNavigation中如何实现页面跳转

    一.ReactNavigation中如何实现页面跳转 因为每个屏幕组件(具有路由地址的组件)都是由App根组件自动创建并挂载的,App组件 在创建屏幕组件时,会自动传递进来一个props:   nav ...