4Sum——LeetCode
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
- Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
- The solution set must not contain duplicate quadruplets.
For example, given array S = {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)
题目大意:跟3Sum类似,这个题是在一个数组中找出4个数的和等于target,如果还用3Sum这种做法,那么复杂度会到O(N^3),效率有点不能忍。
第一种:我想到的第一种方法是枚举a+b的和,放入一个数组,然后对这个数组排序,然后以Binary Search查找target-c-d是否存在于这个数组中,这里有个问题就是排序数组还要记录a、b的下标,只能定义class或者搞个二维数组,时间复杂服是O(N^2*logN)。
To add...
第二种:后来又想到一种方法就是把枚举的a+b的和放入HashMap,以a+b之和作为key,以这两个数的下标作为value,如果分散平均的话这样的时间复杂度是O(N^2),最坏情况是所有的数都一样,那么n^2个数的和只有一个key,List里有n^2个和,退化到O(N^4)。
public List<List<Integer>> fourSum(int[] num, int target) {
List<List<Integer>> res = new ArrayList<>();
if (num == null || num.length < 4) {
return res;
}
int len = num.length;
Map<Integer, List<Integer>> map = new HashMap<>();
Set<String> unique = new HashSet<>();
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
int key = num[i] + num[j];
if (map.get(key) == null) {
List<Integer> list = new ArrayList<>();
list.add(i * len + j);
map.put(key, list);
} else {
List<Integer> list = map.get(key);
list.add(i * len + j);
map.put(key, list);
}
}
}
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
int key = target - num[i] - num[j];
List<Integer> list = map.get(key);
if (list == null || list.isEmpty()) {
continue;
}
for (Integer pos : list) {
int x = pos / len;
int y = pos % len;
if (i == x || i == y || j == x || j == y || x == y)
continue;
int[] t = new int[]{num[i], num[j], num[x], num[y]};
Arrays.sort(t);
String uni = String.valueOf(t[0]) + t[1] + t[2] + t[3];
if (!unique.contains(uni)) {
unique.add(uni);
res.add(Arrays.asList(t[0], t[1], t[2], t[3]));
}
}
}
}
return res;
}
4Sum——LeetCode的更多相关文章
- 4Sum -- LeetCode
原题链接: http://oj.leetcode.com/problems/4sum/ 这道题要求跟3Sum差点儿相同,仅仅是需求扩展到四个的数字的和了.我们还是能够依照3Sum中的解法,仅仅是在外 ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [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 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- leetcode — 4sum
import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...
随机推荐
- [转] JavaScript中的字符串操作
一.概述 字符串在JavaScript中几乎无处不在,在你处理用户的输入数据的时候,在读取或设置DOM对象的属性时,在操作cookie时,当然还有更 多....JavaScript的核心部分提供 ...
- GNU Make----Core Automatic Variables
$@ 表示规则的目标文件名.如果目标是一个文档文件(Linux中,一般称.a 文件为 文档文件,也称为静态库文件),那么它代表这个文档的文件名.在多目标模式 规则中,它代表的是哪个触发规则被执行的目标 ...
- jQuery代码优化 事件委托篇
<转自 http://www.jb51.net/article/28770.htm> 参考文章: 解密jQuery事件核心 - 绑定设计(一) 参考文章: 解密jQuery事件核心 - ...
- HTML5之部分显示
- codevs3304水果姐逛水果街
/* 线段树开到*4 *4 *4 *4 ! 维护 4个值 区间最大值 区间最小值 从左往右跑最大收益 从右往左跑最大收益 */ #include<iostream> #include< ...
- dede织梦后台页面及功能修改及精简操作方法
先让我们来看看都有哪些页面控制着后台的功能和显示.下方为系统默认的后台界面图,为了便于下面的说明我对各个部分进行了一些标示.共A.B.C.D.E五个区域. 常用:A区域[顶部LOGO行]对应文件:/d ...
- 【转】数据库分页Java实现
[转]数据库分页Java实现 MySQL分页 主要是MySQL数据库内置LIMIT函数 注意添加mysql的JAR包mysql-connector-java-5.1.13-bin.jar 在中小数据量 ...
- C# WebService 基础实例
1.整个Demo结构:如下图: 2.新建项目--选择asp.net web服务应用程序TestWebService 3.重新命名Service1.asmx为MyService.asmx 4.右键MyS ...
- 导入excel错误:外部表不是预期的格式 解决方案(Oledb)
-----转载:http://blog.csdn.net/zhou349398998/article/details/8740424 环境:win7+iis7+Office2007 在asp.net网 ...
- UIView之常用方法
UIView之常用方法 将一个视图添加为子视图,并使之在最上面显示 -(void)addSubView:(UIView *)view; 将指定子视图移动到顶部 -(void)bringSubViewT ...