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 ...
随机推荐
- js获取当前url地址及参数
介绍:设置或获取对象指定的文件名或路径. window.location.pathname //返回 设置或获取整个 URL 为字符串. window.location.href 设置或获取与 URL ...
- Web开发 < base target>
target就是点击链接后跳转到的目标页.通俗点说,就是你当前页面上的链接<a>,也包括<img>.<link>.<form>,除非你指定了target ...
- HTML5音乐播放器
//HTML5部分 <a href="javascript:void(0);" onclick="getCurrentTime('firefox');"& ...
- struts2学生信息管理系统篇章③
package com.java1234.util; public class PageUtil { //传进来四个参数,tagetUtil是跳转链接的头部,totalNum是总个数,currentP ...
- java中的IO一
一.IO操作的目标 IO的流向 二.IO的分类方法 1.第一种分法:输入流.输出流 2.第二种分法:字节流.字符流 3.第三种分法:节点流.处理流 三.IO当中的核心类 核心类中的核心方法 Input ...
- 三、T4模板与实体生成
上文我们最后虽然用模板创建了一个实体类,但是类的内容仍旧是静态的,这里我们需要用动态方式生成类的内容.因为需要查询数据库这里又免不了各种繁琐的连接数据库操作,为了使我们的编码更加直观,仍然采 ...
- PHP MySQL 创建数据库
PHP MySQL 创建数据库 数据库存有一个或多个表. 你需要 CREATE 权限来创建或删除 MySQL 数据库. 使用 MySQLi 和 PDO 创建 MySQL 数据库 CREATE DATA ...
- Char Varchar Nvarchar区别
char和varchar是一样的字符型,不同在于,varchar比char更灵活,精确,且不占内存空间,当你取同样的字符时,char会在该字符后面加上空格,而varchar则只取得这个字符,比如有字段 ...
- java_log_01
logback&slf4j(本文中的版本为logback1.1.7.slf4j1.7.21),参照 原作者:Ceki Gülcü.Sébastien Pennec中文版译者:陈华联系方式:cl ...
- SVN 不能提交, 看不到日志, 出现乱码. 解决方案.
需要工具 sprite3: 点这里下载. 解决问题 如本文标题所写. 我遇到过几次一样的问题, 每次都很蛋疼的把目录重新检出, 浪费时间, 又伤了脾气. 下面是我在百度经验找到的一片帖子, 效果杠杠的 ...