[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 ...
随机推荐
- jquery对复选框(checkbox)的操作(精华)
@{ Layout = null;} <!DOCTYPE html> <html><head> <meta name="viewport" ...
- vue和jQuery嵌套实现异步ajax通信
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Python之socket_tcp
1.1socket编程之tcp编程 """ socket类型 sock_stream 面向连接的流套接字,默认值 tcp协议 sock_dgram 无连接的数据报文套接字 ...
- python fabric的用法
1. Fabric的任务运行规则根据Fabric Execution model的说明,fabric默认以串行方式运行tasks,具体而言: 1)在fabfile及其import文件中定义的task对 ...
- python locust 性能测试:locust参数-保证并发测试数据唯一性,循环取数据
from locust import TaskSet, task, HttpLocustimport queue class UserBehavior(TaskSet): @task def test ...
- flask 操作数据时,db的要在app.config设置之后声明:如app.config['SQLALCHEMY_DATABASE_URI']
flask 操作数据时,db的要在app.config设置之后声明:如app.config['SQLALCHEMY_DATABASE_URI'] 否则,运行程序时app.config里面做的设置就不会 ...
- idea下maven下载插件慢问题
在pom.xml中添加maven 依赖包时,我就发现不管是否用了FQ,下载速度都好慢,就1M的东西能下半天,很是苦恼,于是到网上搜资料,然后让我查到了.说是使用阿里的maven镜像就可以了.我于是亲自 ...
- rabbitmq 配置集群镜像
- vi中换行、翻页和查找功能
vi时,按ESC进入Command模式, 1. 移动光标命令:j 向下移动一行:k 向上移动一行:h 向左移动一个字符:l 向右移动一个字符:对于 j.k.l和h键,命令前加数字,移动多行:如 3j, ...
- 浅谈Final
Java关键字final有“这是无法改变的”或者“终态的”含义,final可以修饰非抽象类.非抽象类成员方法和变量. final类不能被继承,没有子类,final类中的方法默认是final的.fina ...