18. 4Sum (JAVA)
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]
]
法I:在3Sum的基础上,多加一个循环。时间复杂度O(n3)。
法II:使用HashTable。O(N^2)把所有pair存入hash表,pair中两个元素的和就是hash值。那么接下来求4sum就变成了在所有的pair value中求 2sum,这个就成了线性算法了。所以整体上这个算法是O(N^2)+O(n) = O(N^2)。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ret = new ArrayList<>();
Map<Integer, List<List<Integer>>> twoSum = new HashMap<>();
Map<Integer, Integer> cnt = new HashMap<>(); //count of each num
//sort array
Arrays.sort(nums);
for(int i = 0; i < nums.length; i++){
//update count
if(cnt.get(nums[i])==null)
cnt.put(nums[i],1);
else
cnt.put(nums[i],cnt.get(nums[i])+1);
//initialize map
if(i>0 && nums[i]==nums[i-1]) continue;//avoid repeat
for(int j = i+1; j < nums.length; j++){
if(j > i+1 && nums[j]==nums[j-1]) continue; //avoid repeat
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[j]);
List<List<Integer>> listGroup = twoSum.get(nums[i]+nums[j]);
if(listGroup ==null ){
listGroup = new ArrayList<>();
}
listGroup.add(list);
twoSum.put(nums[i]+nums[j], listGroup);
}
}
//iterate map to find target sum
Set<Integer> keys = twoSum.keySet();
for (int key : keys) {
List<List<Integer>> listGroupA = twoSum.get(key);
for(List<Integer> listA: listGroupA){
List<List<Integer>> listGroupB = twoSum.get(target - key);
if(listGroupB == null) continue;
for(List<Integer> listB: listGroupB){
//check whether count of num is enough
int a = listA.get(0);
int b = listA.get(1);
int c = listB.get(0);
int d = listB.get(1);
if(Math.max(a,b) > Math.min(c,d)) continue; //四个数两两组合,有6种情况,这里只取两个最小的数在listA的情况,去重
//check count of num
cnt.put(a,cnt.get(a)-1);
cnt.put(b,cnt.get(b)-1);
cnt.put(c,cnt.get(c)-1);
cnt.put(d,cnt.get(d)-1);
if(cnt.get(a) >= 0 && cnt.get(b) >= 0 && cnt.get(c) >= 0 && cnt.get(d) >= 0){
//find one list
List<Integer> listC = new ArrayList<>();
listC.addAll(listA);
listC.addAll(listB);
ret.add(listC);
}
//recover count of num
cnt.put(a,cnt.get(a)+1);
cnt.put(b,cnt.get(b)+1);
cnt.put(c,cnt.get(c)+1);
cnt.put(d,cnt.get(d)+1);
}
}
}
return ret;
}
}
18. 4Sum (JAVA)的更多相关文章
- leetcode 18 4Sum JAVA
题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出 ...
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 1. Two Sum&&15. 3Sum&&18. 4Sum
题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- Java 18套JAVA企业级大型项目实战分布式架构高并发高可用微服务电商项目实战架构
Java 开发环境:idea https://www.jianshu.com/p/7a824fea1ce7 从无到有构建大型电商微服务架构三个阶段SpringBoot+SpringCloud+Solr ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- Java [leetcode 18]4Sum
问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
随机推荐
- Python——PyQt GUI编程(python programming)
import sys from math import * from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidg ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- Go语言操作MySQL数据库
Go语言操作MySQL数据库 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用 ...
- 序列化模块_pickle
序列化: 把不能够直接存储的数据变成字节流(bytes)保存在文件, 进行持久化存储 反序列化: 任何数据都可以转成字节流(bytes)进行存储: 1. dumps 把任意对象序列化 li = [1, ...
- Ehcart整合百度地图
最近上班有些时间,学习了一下Ehcart的知识,自己制作了一份Ehcart整合百度地图的示例代码. GItHub地址:https://github.com/TianYanFd/tianjin-powe ...
- webapi自定义Filter
public class MyAutorFilter : IAuthorizationFilter { public bool AllowMultiple => true; public asy ...
- windows10下 MySQL5.7.18版本安装过程及遇到的问题
windows10下 MySQL5.7.18版本安装过程及遇到的问题 mysql-5.7.18-winx64 安装 1.解压 此次将MySQL装在H盘,依个人喜 ...
- 通过ClickOnce本地打包发布WPF应用程序
环境: 1)VS 2015 Windows10 x64 .net framework 4.5.2 要点: 1)安装模式和设置选择可以脱机使用. 2)系统必备组件中安装位置选择与应用程序相同位置下载系 ...
- NLTK——常用函数
1.Functions Defined for NLTK's Frequency Distributions Example Description fdist = FreqDist(samples) ...
- 使用Keras训练大规模数据集
官方提供的.flow_from_directory(directory)函数可以读取并训练大规模训练数据,基本可以满足大部分需求.但是在有些场合下,需要自己读取大规模数据以及对应标签,下面提供一种方法 ...