题目:

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. 解决phpmyadmin导入大数据库出现一系列问题

    在用phpmyadmin导入mysql数据库文件时,往往超过2M就会提示文件大,导入不成功.这时我们打开phpmyadmin-->libraries-->config.default.ph ...

  2. 常用global.css

    html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldset, legend, img { margin: ...

  3. java中的双重锁定检查(Double Check Lock)

    原文:http://www.infoq.com/cn/articles/double-checked-locking-with-delay-initialization#theCommentsSect ...

  4. SVN 等版本管理工具

    程序猿团队开发代码,必须的程序版本管理工具 1.SVN使用教程总结 2.SVN如何切换用户 在使用svn更新或提交数据时需要输入用户名和密码,在输入框中可以选择是否记录,以便下次操作无需再次输入用户名 ...

  5. redis数据迁移操作

    redis客户端连接命令,分别连接旧环境中的主从redis Src目录下./redis-cli -h IP -p PORT 使用info replication 命令找出主redis使用客户端命令连接 ...

  6. python 基础 1.3 使用pycharm给python传递参数及pycharm调试模式

    一.通过pycharm 给python传递函数 1. 在pycharm终端中写入要获取的参数,进行获取 1>启动pycharm 中Terminal(终端) 窗口 点击pycharm左下角的图标, ...

  7. 九度OJ 1152:点菜问题 (01背包、DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1046 解决:543 题目描述: 北大网络实验室经常有活动需要叫外买,但是每次叫外买的报销经费的总额最大为C元,有N种菜可以点,经过长时间的 ...

  8. pjax + tp5,实现局部无刷新返回数据

    文件1:\application\admin\controller\Setting.php 最后一句代码:要fetch原页面 <?php namespace app\admin\controll ...

  9. JAVA中equals()与==的区别详解

    在进行判断操作时,常常会用到==或者equals()进行等价判断,那么两者究竟有什么区别呢,下面整理一下个人理解. 简单介绍: ==是一种引用相等性比较符,判断引用到堆上同一个对象的两个引用是相等的. ...

  10. 聚聚科技——php开发笔试题及答案

    聚聚科技是一个刚创立的公司,很小很小,人很少,老板感觉是个典型的北京小伙儿,戾气很重,很有个性.笔试题倒是简单: 1. echo(), print(), print_r()的区别? echo是PHP语 ...