四数之和

题目描述:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:答案中不可以包含重复的四元组。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/4sum/

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:双指针法

首先,将nums排序;然后firstfourth指针分别从数组的第一个和最后一位开始,secondthird指针分别从first+1fourth-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-四数之和的更多相关文章

  1. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  2. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

  3. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

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

  5. 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  6. LeetCode 18. 四数之和(4Sum)

    题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...

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

  8. [LeetCode] 18. 四数之和

    题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...

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

  10. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

随机推荐

  1. 尚硅谷全套课件整理:Java、前端、大数据、安卓、面试题

    目录 Java 尚硅谷 IT 精英计划 JavaSE 内部学习笔记.pdf 尚硅谷 Java 基础实战之银行项目.pdf 尚硅谷 Java 技术之 JDBC.pdf 尚硅谷 Java 技术之 Java ...

  2. SharePoint Online 为Modern Page添加脚本

    前言 众所周知,如果我们想向SharePoint 页面添加脚本,最方便的便是经典页面,添加方式主要有内容编辑器Web部件或者直接使用SharePoint Designer. 但是,如果页面是Moder ...

  3. 「ZJOI2017」树状数组

    「ZJOI2017」树状数组 以下均基于模2意义下,默认\(n,m\)同阶. 熟悉树状数组的应该可以发现,这题其实是求\(l-1\)和\(r\)位置值相同的概率. 显然\(l=1\)的情况需要特盘. ...

  4. Android下数据库创建

    什么情况下我们才用数据库做数据存储? 大量数据结构相同的数据需要存储时. sqlite 嵌入式 轻量级 SqliteOpenHelper 创建数据库步骤: 1.创建一个类集成SqliteOpenHel ...

  5. argc 和 argv

    转载请注明来源:https://www.cnblogs.com/hookjc/ 如果用C寫一般的命令列工具,常透過main函式的argc,argv來取得使用者所輸入的命令參數.int main(int ...

  6. 节点流和处理流(BufferedReader和BufferedWriter,BufferedInputStream和BufferedOutputStream,ObjectlnputStream和objectOutputStream)

    一.基本介绍: 1.节点流可以从一个特定的数据源读写数据,如FileReader. FileWriter 如图:字节流是直接对数据源(文件,数组之类存放数据的地方)进行操作 2.处理流(也叫包装流)是 ...

  7. Ubuntu18修改系统时间

    1. 运行 tzselect 依次选择 Asia -> China -> Beijing Time 2. 复制文件到 /etc 下 sudo cp /usr/share/zoneinfo/ ...

  8. MySQL中 BETWEEN ... AND ...

    MySQL中 BETWEEN ... AND ... 1. 准备测试数据 CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...

  9. 第11、12章等待方法和alter框处理

    11.等待方法 time sleep webdriver implicitly_wait() 设置浏览器等待时间 WebDriverWait 等待条件满足或超时后退出   12.alter对话框处理 ...

  10. Java 位运算总结

    一.Java中支持的位运算 位与(&):二元运算符,两个为1时结果为1,否则为0 位或(|):二元运算符,两个其中有一个为1时结果就为1,否则为0 位异或(^):二元运算符,两个数同时为1或0 ...