【题目】

Given an array nums of n integers and an integer target, are there elements abc, 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.

【思路】

和leetcode15三数之和3 Sum类似https://www.cnblogs.com/inku/p/9955638.html

多一次循环,加粗部分是新增的

重点是去重。

【代码】

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> data=new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
for(int j=nums.length-1;j>0;j--){
int left=i+1;
int right=j-1;
if(i>0&&nums[i]==nums[i-1]){
continue;}
if(j<nums.length-1&&nums[j]==nums[j+1]){
continue;}
while(left<right){
int sum=nums[left]+nums[right]+nums[i]+nums[j];
if(sum==target){
data.add(Arrays.asList(nums[left], nums[right],nums[i],nums[j]));
left++;
right--;
while(left<right&&nums[left]==nums[left-1])
left++;
while(left<right&&nums[right]==nums[right+1])
right--;
}
else if(left<right&&sum>target)
right--;
else
left++;
}
}
}
return data;
}
}

[Leetcode 18]四数之和 4 Sum的更多相关文章

  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. 四数之和

    题目链接: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 15]三数之和 3 Sum

    [题目] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...

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

随机推荐

  1. 斐讯面试记录—TCP滑动窗口及拥塞控制

    TCP协议作为一个可靠的面向流的传输协议,其可靠性是由流量控制和滑动窗口协议保证,而拥塞控制则由控制窗口结合一系列的控制算法实现. 一.滑动窗口协议 1. “窗口”对应的是一段可以被发送者发送的字节序 ...

  2. Lua论分析需求(学好英文)的重要性

    题目是这样的: Observe that its base and height are both equal to , and the image is drawn using # symbols ...

  3. 时间序列数据库调研之InfluxDB

    基于 Go 语言开发,社区非常活跃,项目更新速度很快,日新月异,关注度高 测试版本 1.0.0_beta2-1 安装部署 wget https://dl.influxdata.com/influxdb ...

  4. ORA-12805: parallel query server died unexpectedly ORA-04030 (sort subheap,sort key) 原因排查与解决方法

    今日,某服务器pga调整为30G,_pga_max_size调整为8G之后(原来是2G,但是one passes语句较多,性能太低),执行出现ORA-12805: parallel query ser ...

  5. jsp传Array数组到后台

    jsp页面传递对象数组到后台的需求 JSP: //保存 $("#submitBtn").click(function(){ var flag = true; var eachfla ...

  6. 【BZOJ】 4813: [Cqoi2017]小Q的棋盘

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4813 暴力转移就好,考虑以某一个点为根的子树分为是否走回来两种情况 ${f_{i,j}}$ ...

  7. sublime text 3 显示空格和Tab

    tab对于需要 共享 的代码而言是灾难. pyhton对这点的明确要求是不要混合使用4个空格和tab键. 只使用二者之一,都是没有问题的.如果一个python代码已经完成的部分,都是使用的4个空格.那 ...

  8. Java内存分配机制

    内存分配,主要指的是在堆上的分配, 一般的,对象的内存分配都是在堆上进行,但现代技术也支持将对象拆成标量类型(标量类型即原子类型,表示单个值,可以是基本类型或String等),然后在栈上分配,在栈上分 ...

  9. SQL Server 第三章 结构化查询语言

    SQL脚本: create database electric; use electric go create table Salvaging( prj_no int primary key, prj ...

  10. hive新功能cube和rollup

    1.cube简称数据魔方,可以实现hive多个任意维度的查询,cube(a,b,c)则首先会对(a,b,c)进行group by,然后依次是(a,b),(a,c),(a),(b,c),(b),(c), ...