【LeetCode】18、四数之和
题目等级:4Sum(Medium)
题目描述:
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
The solution set must not contain duplicate quadruplets.
Example:
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
题意:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
解题思路:
这个就没什么特别的了,直接参考三数之和:【LeetCode】15、三数之和为0
网上也没有找到什么其他特别的解法,有说采用二分的方法转化为两个两数之和的,但是感觉过于繁琐了,不太直观,这里就直接采用在三数之和的外层又加了一层循环,时间复杂度为:O(n^3).
然后主要对这类题做一个总结:
两数之和系列,做法:
- 若数组无序,采用HashMap,参考:【LeetCode】1、两数之和
- 若数组有序,双指针,参考:【剑指Offer】42、和为S的两个数字
三数之和(N数之和)序列,做法:
- 第一步:java.util.Arrays.sort(int[] nums),升序排列
- 第二步:N-2层for循环,外加两个双向指针,双向指针后需while循环向数组中间靠拢,while循环里嵌套两层while循环,用于去重
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res=new ArrayList<>();
if(nums==null && nums.length==0)
return res;
Arrays.sort(nums);
int len=nums.length;
for(int i=0;i<len-3;i++){
if(i>0 && nums[i-1]==nums[i]) //跳过重复的
continue;
for(int j=i+1;j<len-2;j++){
if(j>i+1 && nums[j]==nums[j-1]) //跳过重复的
continue;
int low=j+1,high=len-1,sum=target-nums[i]-nums[j];
while(low<high){
if(nums[low]+nums[high]==sum){ //找到一个解
res.add(Arrays.asList(nums[i],nums[j],nums[low],nums[high]));
while(low<high && nums[low+1]==nums[low])
low++;
while(low<high && nums[high-1]==nums[high])
high--;
low++;
high--;
}else if(nums[low]+nums[high]<sum)
low++;
else
high--;
}
}
}
return res;
}
}
【LeetCode】18、四数之和的更多相关文章
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 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:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- 【LeetCode】18.四数之和
题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- 【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 相等?找出所有满 ...
随机推荐
- python---win32gui、win32con、win32api:winAPI操作
python操作winAPI 窗口操作: import sys from PyQt5.QtWidgets import QApplication, QWidget from lianxi import ...
- 【Wince-ListView】Wince中的 ListView怎么显示网格?
using System.Runtime.InteropServices; using System.Windows.Forms; namespace CETEST { public class Co ...
- Babel 转译 class 过程窥探--------引用
// Shape 类function Shape(id, x, y) { this.id = id; this.setLocation(x, y);}// 设置坐标的原型方法Shape.p ...
- React组件(组件属性this.state和this.props,css样式修饰组件)
目录: 1.创建组件的第一种方式 function2.将组件抽离为单独的jsx文件3.省略.jsx后缀, 配置webpack设置根目录4.创建组件的第二种方式--使用class关键字创建组件5.组件私 ...
- Listview操作
设置 listView1.VirtualMode = true; listView1.RetrieveVirtualItem += ListView1_RetrieveVirtualItem; p ...
- hdu 5753
Permutation Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 二进制上的数位dpPOJ 3252
Round number POJ - 3252 题目大意:一个"round number" 数的定义是,将它转化成2进制后,0的个数大于等于1的个数,要求的就是在[s,f]范围内& ...
- iOS开发之实现图片自动切换(类似android画廊效果)
#import ViewController.h #define ImageViewCount 5 @interface ViewController ()<uiscrollviewdele ...
- HttpClient : java.net.SocketException: Connection reset
1. 问题排查 httpclient : 4.5.5 排查过程 : 一次SocketException:Connection reset 异常排查 主要原因 : 调用 http 请求时发生了 Sock ...
- 邻居子系统 之 更新neigh_update
概述 neigh_update函数用来更新指定的邻居项,更新内容是硬件地址和状态,更新之后,会根据新状态设置其输出函数,CONNECTED状态则使用快速输出,否则使用慢速输出:如果是由原来的无效状态变 ...