[刷题] 454 4Sum II
要求
- 给出四个整型数组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的更多相关文章
- LeetCode 454. 4Sum II
454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...
- 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 ...
- 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 ...
- 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), ...
- [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 ...
- [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 ...
- leetcode刷题-90子集 II
题目 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2]输出:[ [2], [1], [1,2,2], [ ...
- 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 ...
- 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 ...
随机推荐
- TypeError: Can't convert 'int' object to str implicitly Python常见错误
尝试连接非字符串值与字符串 想要字符串连接非字符串需要先进行强制转化 可以用str()函数 --------------------------------
- 敏捷史话(十一):敏捷宣言“间谍”——Steve Mellor
Steve Mellor 是敏捷宣言的签署人之一,他自称是作为" 间谍"去参加雪鸟会议的. 起初收到会议邀请时,Steve 非常惊讶,因为他所做的工作一直都是关于建模方面的,很少将 ...
- 学习笔记-vue 打包去#和页面空白问题
文件资源路径是对的,但是页面空白.百度了很久找了一篇文章解决了. 1.vue项目中config文件下index.js中打包配置 build: { // Template for index.html ...
- Typescript进阶之路
TypeScript 何为TypeScript 一.编程语言类型 动态类型语言(Dynamically Typed Language) 类型的检查是在运行时才做 例子---JavaScript.Rub ...
- LeetCode剑指Offer刷题总结(一)
LeetCode过程中值得反思的细节 以下题号均指LeetCode剑指offer题库中的题号 本文章将每周定期更新,当内容达到10题左右时将会开下一节. 二维数组越界问题04 public stati ...
- Spring Boot 快速迁移至 Quarkus
Quarkus 是一个目前非常火的 Java 应用开发框架,定位是轻量级的微服务框架.,Quarkus 提供了优秀的容器化整合能力,相较于传统开发框架(Spring Boot)有着更快的启动速度.更小 ...
- Ducci Sequence UVA - 1594
A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1,a2,···,an ...
- math random模块
math --- 数学函数 该模块提供了对C标准定义的数学函数的访问,返回值除非有明确说明,否则所有返回值均为浮点数 math.ceil(x) 返回 x 的上限,即大于或者等于 x 的最小整数. 如果 ...
- 『政善治』Postman工具 — 3、补充:restful风格接口的项目说明
目录 (一)RESTful架构风格特点 1.统一接口风格 2.规范的HTTP请求方法 3.HTTP响应码 4.什么是无状态 (二)JSON数据格式说明 1.什么是JSON 2.JSON格式的特点 3. ...
- 【Springboot】Springboot自动装配原理
1.核心注解就是 EnableAutoConfiguration 该注解会激活SpringBoot的自动装配功能: 代码如下: @Target(ElementType.TYPE) @Retentio ...