4Sum II
https://leetcode.com/submissions/detail/153740275/
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> CD_freq;
for (auto i : C)
for (auto j : D)
CD_freq[i+j]++;
int res = ;
for (auto a : A)
for (auto b : B) {
auto it = CD_freq.find(-(a+b));
if (it != CD_freq.end())
res += it->second;
}
return res;
}
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> CD_freq;
for (auto i : C)
for (auto j : D)
CD_freq[i+j]++;
int res = ;
for (auto a : A)
for (auto b : B)
res += CD_freq[-(a + b)]; // If CD_freq[-(a+b)] doesn't exist, it will be added! So map becomes much bigger!!!
return res;
}
int fourSumCount_n3(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> D_freq;
for (auto i : D)
D_freq[i]++;
int res = ;
for (int i = ; i < A.size(); i++)
for (int j = ; j < B.size(); j++)
for (int k = ; k < C.size(); k++)
res += D_freq[-(A[i] + B[j] + C[k])];
return res;
}
int fourSumCount_n4(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int res = ;
for (int i = ; i < A.size(); i++)
for (int j = ; j < B.size(); j++)
for (int k = ; k < C.size(); k++)
for (int l = ; l < D.size(); l++)
if (A[i] + B[j] + C[k] + D[l] == )
res++;
return res;
}
};
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 ...
- [LeetCode] 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 四数之和之二
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: 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- 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 ...
- [Swift]LeetCode454. 四数相加 II | 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l)there are such th ...
- 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), ...
随机推荐
- varnish pipe 和pass的区别分析
这两天在学习varnish,在学到vcl时,不理解pipe和pass的区别以及如何区分加以应用.通过两天的搜索,总算是理清了概念.现在记录在博客上跟大家分享. 当 vcl_recv 函数接收到请求时, ...
- 64位wampserver开启curl扩展失败的问题
今天在运行程序时报错: Fatal error:Call to undefined function curl_init()... 在网上查了一下,是因为php_curl.dll扩展没有开启的缘故,于 ...
- Soft skill
不要害怕让别人看到自己的无知 作为高级程序员的一个好处是,当别人问一些我不懂的问题时,我可以很淡然地告诉他们: 这个东西我也不懂,因为以前没有遇到过,不过我可以看一下,然后再告诉你. 当我还是一个初级 ...
- linux 解决 gvfsd-smb-browse CPU 100%占用
原文地址:https://bugzilla.redhat.com/show_bug.cgi?id=1303300 1: sudo vim /etc/samba/smb.conf 2:定位到[globa ...
- 学习《CSS选择器Level-4》不完全版
1 概述 1.1 前言 选择器是CSS的核心组件.本文依据W3C的Selectors Level 4规范,概括总结了Level1-Level4中绝大多数的选择器,并做了简单的语法说明及示例演示.希望对 ...
- vue实现选中列表功能
<template> <div> <ul v-for="prop in items"> <dt>{{prop.name}}</ ...
- iOS核心动画高级技巧之图层变换和专用图层(二)
iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结( ...
- 线程池模块thernd
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import time def task(i): print ...
- centos6.5_64bit-nginx安装部署
1.配置防火墙,开启80端口.3306端口 vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dpor ...
- adc verilog spi 时序
我用的是adc081sd芯片,(由于我们使用的是FPGA不用像单片机那样考虑极性cpol,相位cpha,下面仅仅介绍下跟单片机比较下) 什么是cpol:若cs被拉为低电平时sclk(时钟)是高那么cp ...