题目:

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.

To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.

Example:

Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2] Output:
2 Explanation:
The two tuples are:
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

  

题解:

  嗯,老规矩,直接暴力解,一共四个循环。

Solution 1 (TLE)

 class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
sort(A.begin(), A.end());
sort(B.begin(), B.end());
sort(C.begin(), C.end());
sort(D.begin(), D.end());
int n = A.size();
int cnt = ;
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
for(int k=; k<n; k++) {
for(int l=; l<n; l++) {
int sum = A[i] + B[j] + C[k] + D[l];
if(sum==) {
cnt++;
}
else if(sum<) continue;
else break;
}
}
}
}
return cnt;
}
};

  不出意外,TLE了,哈哈。那怎么降低时间复杂度呢?我基本上第一时间想到的就是双指针和map(set),这个明显双指针一般用于单个数组的操作,因此可以考虑使用map。思路:使用两个循环,循环一:建立两个map,一个是AB两数组元素和与出现次数的map,另一个为CD两数组元素和与出现次数的map,循环二:遍历map1,map2使用find函数来查找是否存在遍历元素的相反数。

Solution 2 (706ms)

 class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> m1, m2;
int result = ;
int n = A.size(); for(int i = ; i < n; ++i) {
for(int j = ; j < n; ++j) {
++m1[A[i] + B[j]];
++m2[C[i] + D[j]];
}
}
/* for(auto it = m1.begin(); it != m1.end(); ++it) {
if(m2.find(-it->first) != m2.end())
result += it->second * m2[-it->first];
} */
for (auto p : m1) result += p.second * m2[-p.first];
return result;
}
};

  另一个解法也是利用两个循环,不过只使用一个map,建立AB两数组元素和与出现次数的map后,对CD遍历元素求和得opp,result += m[-opp],若-opp不存在,则m[-opp]为0,若存在,则result加上其出现的次数。

Solution 3 (449ms)

 class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int result = , n = A.size();
unordered_map<int, int> m;
for (int i=; i<n; ++i) {
for (int j=; j<n; ++j) {
++m[A[i] + B[j]];
}
}
for (int i=; i<n; ++i) {
for (int j=; j<n; ++j) {
int opp = - * (C[i] + D[j]);
result += m[opp];
}
}
return result;
}
};

【LeetCode】454 4Sum II的更多相关文章

  1. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  2. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  3. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  4. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  5. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  6. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  7. 【LeetCode】基本计算器II

    [问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  .整数除法仅保留整数部分. 输入: "3+2*2" ...

  8. 【LeetCode 】N皇后II

    [问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...

  9. 【LeetCode】跳跃游戏II

    [问题]给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [,,,,] 输出: ...

随机推荐

  1. Java 调用OPENOFFIC 转换文档类型

    public static void office2PDF(String sourceFile, String destFile) { try { File inputFile = new File( ...

  2. Android 自己定义ImageView实现圆角/圆形 附加OnTouchListener具体凝视以及Button圆角

    转载请注明出处:王亟亟的大牛之路 平时要用一些非方方正正的button之类的小伙伴们是怎样实现的?RadioButton? ImageButton? 还是其它? 今天亟亟上的是ImageView来实现 ...

  3. jQuery--基础(操作标签)

    jQuery-样式操作 .css() 可以直接使用来获取css的值   .css("color")     使用方法,如果想给查找到的标签添加样式: .css("colo ...

  4. 小练习:用socket实现Linux和Windows之间的通信

    在日常生活中,绝大部分人使用的机器通常是windows系统,可是对于研发人员,开发.编译等工作往往是建立在linux机器上.其实.在服务器方面,Linux.UNIX和WindowsServer占领了市 ...

  5. nginx配置1:借助Nginx搭建反向代理服务器与缓存静态文件

    修改配置文件nginx.conf (1)进程数与每个进程的最大连接数: •nginx进程数,建议设置为等于CPU总核心数 •单个进程最大连接数,那么该服务器的最大连接数=连接数*进程数 (2)Ngin ...

  6. BillBoardView自己定义控件广告板轮播

    BillBoardView自己定义控件广告板轮播 GitHub地址 compile 'com.march.billboardview:billboardview:2.0.6-beta4' BillBo ...

  7. 【JavaSE】Java问题总结

    使用BufferedInputStream时OutOfMemoryError异常 eclipse Luna安装subversion(SVN) 使用BufferedInputStream时OutOfMe ...

  8. C#泛型<T>说明

    泛型:即通过参数化类型来实现在同一份代码上操作多种数据类型.泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用. C#泛型的作用概述 C#泛型赋予了代码更强的类型安全,更 ...

  9. Java 并发随身记(一)之 Unsafe类

    最近在看Java并发相关的内容,需要自己整理整理,不然就生疏了.工作2年多,工作时一般注都是框架.消息这些内容,对基础内容比较忽视.闲话不说,既然是并发内容,首先先复习一下Unsafe的内容吧. Un ...

  10. Node.js面试题

    Node.js面试题列表 什么是错误优先的回调函数? 如何避免回调地狱? 如何用Node来监听80端口? 什么是事件循环? 哪些工具可以用来保证一致的编程风格? 什么是测试金字塔?对于HTTP API ...