[Leetcode 18]四数之和 4 Sum
【题目】
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.
【思路】
和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的更多相关文章
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
- LeetCode 18. 四数之和(4Sum)
题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- LeetCode:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- 【LeetCode】18.四数之和
题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- [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? ...
- 【LeetCode】四数之和
[问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...
- [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 ...
随机推荐
- caffe-ssd的GPU安装时make test 报错:.build_release/test/test_all.testbin:
报错原因:LIBRARIES路径添加不全 解决方法:LIBRARIES += glog gflags protobuf boost_system boost_filesystem boost_rege ...
- git 常用命令收集
1. 查看某文件的历史递交记录git log --pretty=oneline 文件名 2. 查看远程仓库信息 git remote show origin 3. 查看用户名和修改用记名: git c ...
- flask重要点
django与flask的区别 django: 大而全的框架,包含了很多组件,例如:ORM.form.ModelForm.session... flask: 轻量级的可扩展强的框架.有丰富的第三方组件 ...
- Electron把网页打包成桌面应用并进行源码加密
前言 最近想把自己用html+css+js做的网页界面打包成桌面应用,网上一搜,发现Electron是一个不错的选择,试了试,发现效果真的不错.这里记录一下打包过程以作记录,便于自己以后查看学习. 一 ...
- 《CSS世界》读书笔记(六)
<!-- <CSS世界> 张鑫旭著 --> min-width/max-width和min-height/max-height min-width/max-width出现的场景 ...
- Jmeter接口测试+压力测试+环境配置+证书导出
jmeter是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单.因为jmeter是java开发的,所以运行的时候必须先要安 ...
- redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: 断开的管道 (Write failed)
昨晚,包发到测试环境中,出现redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: 断开的 ...
- [c/c++] programming之路(18)、动态分配内存malloc
一.图解堆栈 #include<stdio.h> #include<stdlib.h> #include<Windows.h> void main0(){ **]; ...
- Learning-Python【10】:函数初识
一.什么是函数 函数就是代码的一种组织形式,是指将一组语句的集合通过一个名字(函数名)封装起来,要想指向这个函数,只需要调用其函数名即可 函数分为两大类:内置函数 和 自定义函数 二.为何要用函数 减 ...
- Linux常用命令——文件搜索命令
Linux常用命令——文件搜索命令 Linux 以#号开头的内容都是内容描述或配置项 find 描述:文件搜索 语法:find [搜索范围] [范围条件] . 当前目录 示例:[root@local ...