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 ...
随机推荐
- Lesson7——Pandas 使用自定义函数
pandas目录 简介 如果想要应用自定义的函数,或者把其他库中的函数应用到 Pandas 对象中,有以下三种方法: 操作整个 DataFrame 的函数:pipe() 操作行或者列的函数:apply ...
- halcon视觉入门钢珠识别
halcon视觉入门钢珠识别 经过入门篇,我们有了基础的视觉识别知识.现在加以应用. 有如下图片: 我们需要识别图片中比较明亮的中间区域,有黑色的钢珠,我们需要知道他的位置和面积. 分析如何识别 编写 ...
- 使用 Frp 和 Docker 通过远程桌面和 SSH 来远程控制 Windows(反向代理)
最新博客文章链接 大体思路 使用 Docker 容器,在云服务器上部署 Frps 容器来中转流量,在被控制的 Windows 上部署 Frpc 容器来暴露内网的服务,在主控制端的 Windows 上直 ...
- HuTool工具包操作csv文件
CsvUtil是CSV工具类,主要封装了两个方法: getReader 用于对CSV文件读取 getWriter 用于生成CSV文件 1.读取文件 读取为CsvRow CsvReader reader ...
- C++中三种正则表达式比较(C regex,C ++regex,boost regex)
工作需要用到C++中的正则表达式,以下三种正则可供参考 1,C regex #include <regex.h> #include <iostream> #include &l ...
- select 级联选择
转载请注明来源:https://www.cnblogs.com/hookjc/ <script language="javascript"> <!-- ...
- IO复习
/* 字节流 输入字节流: ---------| InputStream 所有输入字节流的基类. 抽象类 ------------| FileInputStream 读取文件的输入字节流 ------ ...
- mapTest
import java.util.*;public class mapTest { public static void main(String[] args) throws Exception{ L ...
- squid 代理服务器应用
squid 代理服务器应用 1.Squid 代理服务器 : Squid 主要提供缓存加速.应用层过滤控制的功能. 代理的工作机制: 代替客户机向网站请求数据,从而可以隐藏用户的真实IP地址. 将获得 ...
- MLlib学习——降维
降维(Dimensionality Reduction) 是机器学习中的一种重要的特征处理手段,它可以减少计算过程中考虑到的随机变量(即特征)的个数,其被广泛应用于各种机器学习问题中,用于消除噪声.对 ...