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 ...
随机推荐
- Working hard to know your neighbor's margins:Local descriptor learning loss论文笔记
Abstract 论文提出了一种新的训练方法,受到了 Lowe's matching criterion for SIFT的启发.这种新的loss,要比负责的正则方法更好.把这个新的loss方法结合L ...
- atomic 原子自增工程用法案例
案例 1 : 简单用法 atomic_int id; atomic_fetch_add(&id, 1) atomic_uint id; atomic_fetch_add(&id, 1) ...
- 「ZJOI2014」璀灿光华
「ZJOI2014」璀灿光华 实际上,可以不用建水晶立方体... 因为,发光水晶的方向都要枚举一遍. 只需知道发光水晶每个方向有哪些水晶就可以了. 对于一个发光水晶,将它连接的水晶标号. 从该水晶bf ...
- MySQL不建议delete删除数据
InnoDB存储架构 从这张图可以看到,InnoDB存储结构主要包括两部分:逻辑存储结构和物理存储结构. 逻辑上是由表空间tablespace -> 段segment或者inode -> ...
- Android生成xml
XmlSerializer //使用XmlSerializer来序列化xml文件 public static boolean backupSms_android(Context con ...
- 广播接收者案例_ip拨号器
(1)定义一个类继承BroadCastReceiver public class OutGoingCallReceiver extends BroadcastReceiver { //当接收到外拨电话 ...
- CentOS 6.4x64安装部署zabbix-2.4.5
以下内容来自于http://www.iyunv.com/thread-62087-1-1.html 补充一点,按照原文安装万之后zabbix页面会提示 zabbix server is not run ...
- Python—列表元组和字典
Python-列表元组和字典 列表 元组 字典 列表: 列表是Python中的一种数据结构,他可以存储不同类型的数据.尽量存储同一种类型 列表索引是从0开始的,我们可以通过索引来访问列表的值. 列表的 ...
- spring security中当已登录用户再次访问登录界面时,应跳转到home
@RequestMapping("/login") public String login(){ Authentication auth = SecurityContextHold ...
- PyTorch图文安装教程(Win10),含遇到的问题及解决办法
PyTorch安装教程(Win10),含遇到的问题及解决办法 1. 环境准备 首先,打开PyTorch官网:https://pytorch.org/get-started/locally/ 向下可以看 ...