【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 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的更多相关文章
- 【LeetCode】454. 4Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】基本计算器II
[问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 .整数除法仅保留整数部分. 输入: "3+2*2" ...
- 【LeetCode 】N皇后II
[问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...
- 【LeetCode】跳跃游戏II
[问题]给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [,,,,] 输出: ...
随机推荐
- C语言日期计算器
记录下码子 # define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <stdlib.h> int days ...
- Tomcat 7.0 servlet @WebServlet
在使用tomcat7.0+eclipse j2ee时,新建Dynamic Web Project时, 会让选择是否生成web.xml.无论选择与否,此时新建一个servlet, 可以不在web.xml ...
- java eclipse使用不同jdk版本
因为开发需要,两个工程分别需要使用jdk1.6(elipse indigo)和jdk1.8(eclipse neon).因为两个eclipse对于jdk版本的要求不同,若只在环境变量中配置jdk版本, ...
- HDU 5338(ZZX and Permutations-用线段树贪心)
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- OIer同样是音乐家
烦闷的时候,shenben为大家准备了2首歌(不用耳机也能听哦) 只需把代码复制到dev-c++的编辑器上,轻按F11,然后聆听OIer的音乐…… 千本樱 曲谱 #include <cstdio ...
- EasyDSS流媒体解决方案之多方式虚拟直播方法
EasyDSS_Solution虚拟直播 EasyDSS_Solution虚拟直播,是EasyDSS流媒体解决方案提供的虚拟直播方案.可以通过三种方式创建虚拟直播. (1)点播的视频文件: (2)本地 ...
- TCP(Socket基础编程)
1.TCP特点: 面向连接.可靠安全.传输量大.速度较慢 2.socket编程主要依靠两个类:socket .serversocket example1:客户端可以不停输入字符串,服务端返回字符串的大 ...
- 原型模式 private static Map<String,Prototype> map = new HashMap<String,Prototype>();
public class PrototypeManager { /** * 用来记录原型的编号和原型实例的对应关系 */ private static Map<String,Prototype& ...
- C#获取网页内容的三种方式(转)
搜索网络,发现C#通常有三种方法获取网页内容,使用WebClient.WebBrowser或者HttpWebRequest/HttpWebResponse... 方法一:使用WebClient (引用 ...
- 九度OJ 1006:ZOJ问题 (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18621 解决:3197 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. ...