[Leetcode] 454. 4Sum II 解题报告
来源:互联网 发布:catti含金量 知乎 编辑:程序博客网 时间:2024/05/22 16:47
题目:
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:2Explanation:The two tuples are:1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 02. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
思路:
1、四重循环法(暴力法):最直观的思路,时间复杂度是O(n^4),空间复杂度是O(1),不过应该过不了大数据测试。
2、三重循环法:可以首先对元素进行排序,然后利用C和D的单调性,将k和l放在同一个循环里面。这样时间复杂度可以降低到O(n^3)。如果不使用哈希表,则算法的空间复杂度仍然是O(1)。
3、二重循环法:可以两个两个的求元素和,也就是先求出A,B数组的任意两个元素和放到hash表中,然后在计算C, D任意两个元素和的时候查找一下hash看能不能找到C, D这两个元素的负数,如果可以找到说明这四个元素相加是为0的。还需要注意的重复元素,在做hash的时候value为个数即可。这样时间复杂度降为O(n^2), 当然空间复杂度会增加O到(n^2)。
代码:
2、三重循环法:
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 size = A.size(), ret = 0; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { int sum = 0 - A[i] - B[j]; int k = 0, l = size - 1; while (k < size && l >= 0) { int value = C[k] + D[l]; if (value == sum) { int k_count = 1; while (k + 1 < size && C[k] == C[k + 1]) { ++k_count, ++k; } int l_count = 1; while(l - 1 >= 0 && D[l] == D[l - 1]) { ++l_count, --l; } ret += k_count * l_count; ++k, --l; } else if (value < sum) { ++k; } else { --l; } } } } return ret; }};
3、二重循环法:
class Solution {public: int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { unordered_map<int, int> hash1; int ans = 0; for(int i = 0; i < A.size(); i++) { for(int j = 0; j < B.size(); j++) { ++hash1[A[i]+B[j]]; } } for(int i = 0; i < C.size(); i++) { for(int j = 0; j < D.size(); j++) { ans += hash1.count(-C[i] - D[j])? hash1[-C[i] - D[j]] : 0; } } return ans; }};
阅读全文
0 0
- [Leetcode] 454. 4Sum II 解题报告
- 【LeetCode】Path Sum II 解题报告
- 【LeetCode】Combination Sum I & II 解题报告
- LeetCode解题报告--Combination Sum II
- [leetcode] 40. Combination Sum II 解题报告
- [leetcode] 113. Path Sum II 解题报告
- [LeetCode] Path Sum II 解题报告
- leetcode 40. Combination Sum II 解题报告
- LeetCode 167.Two Sum II 解题报告
- [Leetcode] 40. Combination Sum II 解题报告
- [Leetcode] 113. Path Sum II 解题报告
- [LeetCode]4Sum, 解题报告
- 【LeetCode】4Sum 解题报告
- LeetCode解题报告--4Sum
- [LeetCode] 4Sum 解题报告
- Leetcode #40. Combination Sum II 组合求和2 解题报告
- [leetcode] 364. Nested List Weight Sum II 解题报告
- Leetcode 113. Path Sum II 路径和2 解题报告
- 网络管理软件在高校网络中的应用
- tp3.2结合mysql实现微信自定义分享链接和图文
- Node.js是什么?
- Kotlin学习笔记--继承、接口、代理、委托、单例
- nginx新手指南
- [Leetcode] 454. 4Sum II 解题报告
- 周志华《Machine Learning》学习笔记(16)--概率图模型
- C++ 变量类型
- 通过Ajax方式上传文件,使用FormData进行Ajax请求
- 二叉搜索树的原理和实现
- android中图片压缩分析(下)
- Hadoop2.5.2+Zookeeper3.4.6 + HBase0.98.8-hadoop2环境搭建
- python机器学习
- count(*) 和 count(1)和count(列名)区别