LeetCode-018-四数之和
四数之和
题目描述:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:答案中不可以包含重复的四元组。
示例说明请见LeetCode官网。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/4sum/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一:双指针法
首先,将nums排序;然后first和fourth指针分别从数组的第一个和最后一位开始,second和third指针分别从first+1和fourth-1处从两边向内移动,直到second不小于third,移动过程中需要判断4个指针所指向的数字之和是否和target相等,如果相等,则放到结果集result里面。 直到遍历到first不小于fourth-2为止,最后返回结果result。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public static List<List<Integer>> fourSum(int[] nums, int target) {
if (nums == null || nums.length < 4) {
return new ArrayList<>();
}
if (nums.length == 4 && nums[0] + nums[1] + nums[2] + nums[3] != target) {
return new ArrayList<>();
}
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for (int first = 0, fourth = nums.length - 1; first < fourth - 2; first++) {
// 过滤掉重复的
if (first > 0 && nums[first] == nums[first - 1]) {
continue;
}
while (first < fourth - 2) {
// 过滤掉重复的
if (fourth < nums.length - 1 && nums[fourth] == nums[fourth + 1]) {
fourth--;
continue;
}
for (int second = first + 1, third = fourth - 1; second < third; second++) {
// 过滤掉重复的
if (second > first + 1 && nums[second] == nums[second - 1]) {
continue;
}
while (second < third && nums[first] + nums[second] + nums[third] + nums[fourth] > target) {
third--;
}
if (second != third && nums[first] + nums[second] + nums[third] + nums[fourth] == target) {
result.add(new ArrayList<>(Arrays.asList(new Integer[]{nums[first], nums[second], nums[third], nums[fourth]})));
}
}
fourth--;
}
fourth = nums.length - 1;
}
return result;
}
public static void main(String[] args) {
int[] nums = new int[]{-3, -1, 0, 2, 4, 5};
for (List<Integer> integers : fourSum(nums, 0)) {
for (Integer integer : integers) {
System.out.print(integer + "/");
}
System.out.println();
}
}
}
【每日寄语】忠实的守住自己最初的梦想,让生活的每一天都变得有意义。
LeetCode-018-四数之和的更多相关文章
- 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】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
随机推荐
- python 小兵(6)函数进阶
阅读目录 函数参数-动态参数 名称空间 函数的嵌套 gloabal.nonlocal 回到顶部 函数参数-动态参数 之前我们说过传参,如果我们在传参数的时候不很清楚有哪些的时候,或者说给一个函数传了很 ...
- PyCharm专业版破解教程
破解步骤: 1. 下载激活包和注册码:https://pan.baidu.com/s/1_1nrQdTElf4aEg8vGnMTzg 2. 将下载的破解包放入PyCham安装目录的bin文件夹中 3. ...
- 开发升讯威在线客服系统启示录:怎样编写堪比 MSDN 的用户手册
本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程. 免费在线使用 & 免费私有化部署:https://kf.shengxunwei.com 文章目 ...
- C语言读取写入CSV文件 [一]基础篇
本系列文章目录 [一] 基础篇 [二] 进阶篇--写入CSV [三] 进阶篇--读取CSV 什么是CSV? CSV 是一种以纯文本形式存储的表格数据,具体介绍如下(来自维基百科): 逗号分隔值(Com ...
- X000101
P3879 [TJOI2010]阅读理解 考虑用 Trie 解决 #include<stdio.h> #include<bitset> #include<string.h ...
- AGC004 部分简要题解
E 首先问题可以转化为:每次将出口带着边界走,出了边界的机器人立马消失,最大化出口碰到的机器人数量. 考虑哪些机器人是已经出界了的,不难有观察: 当前出界的机器人只与当前出口往四个方向走过的最远距离有 ...
- JDBC 操作预编译语句中LIKE模糊匹配怎么用
问题描述 在使用JDBC 预编译执行语句时,遇到一个问题,那就是在含有LIKE的查询语句时,我到底怎么使用匹配符%._呢. 如: SELECT * FROM "+LQ_USERS+" ...
- samb建立共享文件夹,windows报无法访问没有访问权限
一.首先确保你已经成功安装上了samba 二.在修改smb.conf文件之前,先拷贝一个备份,然后输入下面的命令修改smb.conf gedit /etc/samba/smb.conf将smb.con ...
- Java高质量博文链接集合
1. 看完这个,Java IO从此不在难 https://www.jianshu.com/p/715659e4775f 2. Java 8 中的 Streams API 详解 https://www. ...
- 一招教你IDEA中Java程序如何打包,以及打包后如何运行
前言 编写程序 程序打包 测试运行 IDEA作为目前按最主流的Java程序项目编写工具,越来越受到开发人员的青睐.idea因为其五花八门的功能,让你在开发过程中效率显著提高.那么对于初学者来说,如何通 ...