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. vue项目中使用bpmn-流程图xml文件中节点属性转json结构

    内容概述 本系列“vue项目中使用bpmn-xxxx”分为七篇,均为自己使用过程中用到的实例,手工原创,目前陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意事项,具 ...

  2. Git与GitHub常用操作

    --------------------------基本操作--------------------------clone 拷贝远程仓库commit 本地提交push 远程提交pull 更新本地--- ...

  3. JavaWeb学习之JSP(三) EL表达式

    EL表达式 什么是EL表达式   EL,Expression Language,表达式语言,是一种在JSP页面中获取数据的简单方式,通过${变量名}的方式可以获取到值,需要注意的是EL只能从 page ...

  4. java 获取请求ip,服务本地ip

    /** * 获取请求IP * * @param request * @return */ public static String getRequestIpAddress(HttpServletReq ...

  5. express+mysql实现简单的登录功能

    登录页面图: node.js文件代码: const express=require("express"); const app=express(); const path=requ ...

  6. 【遗传编程/基因规划】Genetic Programming

    目录 背景介绍 程序表示 初始化 (Initialization) Depth定义 Grow方法 Full方法 Ramped half-and-half方法 适应度(Fitness)与选择(Selec ...

  7. CPU 多核指令 —— WFE 原理【原创】

    转自:http://tinylab.org/arm-wfe/ Zhang Binghua 创作于 2020/05/19 打赏 微信公众号   知识星球 关注 @泰晓科技 与数千位一线 Linux 工程 ...

  8. RN概述

    一.RN概述 中文网:http://reactnative.cn/ ReactNative:使用JS语法编写移动APP应用,RN会把JS转换为底层Java或OC, 最终运行于手机-------完全不依 ...

  9. strut2登陆注册验证码

    1. 生成图片和验证码 package com.jmu.code; import java.awt.Color; import java.awt.Font; import java.awt.Graph ...

  10. Palindromes _easy version(hdu2029)

    输入格式:首先一个整型,然后循环不带空格未知长度的字符串. 思考:首先用scanf_s()输入整型,然后一个大循环,用gets_s()函数输入字符串. 注意:scanf_s()多加了一个%c,& ...