LeetCode--018--四数之和(java)
给定一个包含 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]
]
和三数之和的区别就是,在外层多了一层for循环。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
if(nums.length < 4)return res;
Arrays.sort(nums);
for(int i = 0;i < nums.length - 3;i++){
if(i > 0 && nums[i] == nums[i-1])continue;
for(int j = i + 1;j < nums.length - 2;j++){
if(j > i + 1 && nums[j] == nums[j-1]) continue;
int low = j + 1,high = nums.length - 1;
while(low < high){
int sum = nums[i] + nums[j] + nums[low] + nums[high];
if(sum == target){
res.add(Arrays.asList(nums[i],nums[j],nums[low],nums[high]));
while(low < high && nums[low] == nums[low+1])low++;
while(low < high && nums[high] == nums[high-1]) high--;
low++;
high--;
}else if(sum < target){
low++;
}else high--;
}
}
}
return res;
}
}
2019-04-17 21:23:25
用python按上面的写一遍,发现根本过不了,要提前停止搜索,所以又加了四个if
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
n = len(nums)
if n<4:
return []
nums.sort()
res = []
for i in range(n-3):
if i>0 and nums[i]==nums[i-1]:continue
if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target):
break
if(nums[i]+nums[-1]+nums[-2]+nums[-3]<target):
continue
for j in range(i+1,n-2):
if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target):
break
if(nums[i]+nums[j]+nums[-1]+nums[-2]<target):
continue
if j>i+1 and nums[j]==nums[j-1]:continue
L = j+1
R = n-1
while L<R:
print(R)
temp = nums[i]+nums[j]+nums[L]+nums[R]
if temp == target:
res.append([nums[i],nums[j],nums[L],nums[R]])
while L<R and nums[L]==nums[L+1]:
L+=1
while L<R and nums[R]==nums[R-1]:
R-=1
L+=1
R-=1
elif temp < target:
L+=1
else:
R-=1
return res
2019-11-30 08:32:54
LeetCode--018--四数之和(java)的更多相关文章
- LeetCode:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
- 【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 ...
- 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】
给定一个包含 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]四数之和 4 Sum
[题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- [LeetCode] 454. 4Sum II 四数之和II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
随机推荐
- pytorch 中的重要模块化接口nn.Module
torch.nn 是专门为神经网络设计的模块化接口,nn构建于autgrad之上,可以用来定义和运行神经网络 nn.Module 是nn中重要的类,包含网络各层的定义,以及forward方法 对于自己 ...
- 设计模式之——bridge模式
Bridge模式,又叫桥接模式,是针对同一接口进行扩展与实现操作的一种设计模式. 这种模式,与之前学过的适配器模式具有相似的地方,也有不同的地方,下面就让我们一一解析吧. 首先,我们要了解到,为什么需 ...
- js--单选按钮赋值
var sex='${userInfo.sex}'; if(sex=="女"){ $("input[name=sex][value='女']").attr(&q ...
- session_unset 与 session_destroy 区别
session_unset() 释放当前在内存中已经创建的所有$_SESSION变量,但不删除session文件以及不释放对应的session id session_destroy() 删除当前用户对 ...
- tarjan代码
还有五天就是NOIP2018了……本蒟蒻还要复习期中考试,因此实在没有时间写博客了(各种找借口).这里就放一下代码 //Tarjan缩点 //题目描述:给一个有向图.每个点有一个权值,求权值和最大的路 ...
- MACD技术的高级应用--MACD与波浪
在开始分析MACD指标之前,我想我们必须先从思想上认同以下两点,否则本文的研究就没有意义. 1)趋势在一段时间内是可以把握的: 2)每个指标都有有效的时候,没有指标会始终有效.我们就是要搞清楚指标 ...
- 六种方式读取properties资源文件
conf.properties文件内容: reportStationName=xx供电局 JBM=0318 文件路径: 其中xxx为项目名 import java.io.BufferedInputSt ...
- Centos安装Oracle及问题处理
安装Oracle前准备 创建运行oracle数据库的系统用户和用户组 [jonathan@localhost ~]$ su root #切换到root Password: [root@localhos ...
- C#构造函数、私有构造函数、静态构造函数与构造函数执行顺序
默认构造函数,如果没有为类指定任何构造函数,编译器会自动为类创建一个无参构造函数,用以初始化类的字段:如果为类编写了构造函数,那么编译器就不会再自动生成无参构造函数了.ps.C#不允许用户为结构定义无 ...
- DX9 DirectX 索引缓存(IndexBuffer) 代码
// @time: 2012.3.22 // @author: jadeshu // des: 索引缓存 //包含头文件 #include <Windows.h> #include < ...