要求

  • 给出四个整型数组ABCD,寻找有多少 i j k l 的组合,使得A[i]+B[j]+C[k]+D[l]=0
  • ABCD元素个数均为N,0<=N<=500

示例

  • 输入:

  A = [ 1, 2]

  B = [-2,-1]

  C = [-1, 2]

  D = [ 0, 2]

  • 输出:2

  1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0

  2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

思路

  • 暴力解法,遍历A+B+C+D(n4,约63亿次运算)
  • 将D中元素放入查找表,遍历A+B+C(n3,约1.25亿次运算,一般计算机难以在1s内完成)
  • 将C+D的每一种可能放入查找表,遍历A+B(n2,25万个元素的查找表,25万次运算)
  • 和可能重复,使用map,记录每个和出现了多少次
 1 class Solution {
2 public:
3 int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
4
5 unordered_map<int,int> record;
6 for( int i = 0 ; i < C.size() ; i ++ )
7 for( int j = 0 ; j < D.size() ; j ++ )
8 record[ C[i] + D[j] ] ++;
9
10 int res = 0;
11 for( int i = 0 ; i < A.size() ; i ++ )
12 for( int j = 0 ; j < B.size() ; j ++ )
13 if( record.find( 0 - A[i] -B[j] ) != record.end() )
14 res += record[ 0 - A[i] -B[j] ];
15
16 return res;
17 }
18 };

相关

  • 49 Group Anagrams

[刷题] 454 4Sum II的更多相关文章

  1. LeetCode 454. 4Sum II

    454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...

  2. 454. 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  3. LC 454. 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  4. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  5. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  6. [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 ...

  7. leetcode刷题-90子集 II

    题目 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2]输出:[ [2], [1], [1,2,2], [ ...

  8. 454. 4Sum II ——查找本质:hash最快,二分次之

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  9. LeetCode 454. 4Sum II (C++)

    题目: Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are su ...

随机推荐

  1. java进阶(41)--反射机制

    文档目录: 一.反射机制的作用 二.反射机制相关类 三.获取class的三种方式 四.通过反射实例化对象 五.通过读属性文件实例化对象 六.通过反射机制访问对象属性 七.通过反射机制调用方法 ---- ...

  2. leetcode 刷题(数组篇)74 题 搜索二维矩阵 (二分查找)

    二分查找要注意边界值的取值,边界情况的判定 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一 ...

  3. Java后端进阶-网络编程(Netty责任链Pipeline)

    设计模式-责任链模式 一个责任链模拟demo package com.study.hc.net.netty.demo; // -----链表形式调用------netty就是类似的这种形式 publi ...

  4. malloc 函数分析 glibc2.23

    malloc 函数详解 本篇主要是参考了glibc 2.23的源码 首先我们来看看malloc函数的执行流程. strong_alias (__libc_malloc, __malloc) stron ...

  5. IntelliJ IDEA/Android Studio插件开发指南

    前言 目前在为安卓手机QQ做自动化的相关工作,包括UI自动化,逻辑层自动化等.使用到的uiautomator等框架,需要在Android Studio进行编码工作. 其中很多工作如果做到插件化的话,可 ...

  6. (十七)VMware Harbor 垃圾清理

    1. 在线垃圾清理 注意:从Harbor中删除镜像时不释放空间,垃圾收集是通过从清单中不再引用文件系统中删除blob来释放空间的任务. 注意:在执行垃圾收集时,Harbor将进入只读模式,并且禁止对d ...

  7. JAVAEE_Servlet_10_HTTP协议

    HTTP协议 * 什么是HTTP 协议? - HTTP协议(超文本传输协议HyperText Transfer Protocol),它是基于TCP协议的应用层传输协议,简单来说就是客户端和服务端进行数 ...

  8. Day12_59_Java多线程

    多线程 1. 什么是进程? * 每个进程是一个应用程序,都有独立的内存空间,一个进程对应一个应用程序. * 例如:在windows操作系统中启动了word就是启动了一个进程,一边听音乐,一边打游戏就是 ...

  9. ambari介绍及安装

    Ambari简介 Ambari概述 Apache Ambari是一种基于Web的工具,支持Apache Hadoop集群的创建.管理和监控.Ambari已支持大多数Hadoop组件,包括HDFS.Ma ...

  10. G - Number Transformation(BFS+素数)

    In this problem, you are given an integer number s. You can transform any integer number A to anothe ...