923. 3Sum With Multiplicity - LeetCode
Question

Solution
题目大意:
给一个int数组A和一个目标值target,求满足下面两个条件的组合个数,其中i,j,k分别为数组的索引
- i<j<k
- target = A[i] + A[j] + A[k]
思路:
# step1 统计数组中每个元素出现的次数
一个元素I出现的次数记为count(I)
# step2 分类
记:I=A[i], J=A[j], K=A[k], C(count(I),3) 表示从count(I)个数中取3个的组合数
I=J=K C(count(I), 3)
I=J!=K C(count(I), 2) * count(K)
I!=J=K count(I) * C(count(K), 2)
I!=J!=K count(I) * count(J) * count(K)
# 复杂度
Time complexity: O(n + target^2)
Space complexity: O(100)
下面是参考视频中的截图,其中的i,j,k是数而不是index

Java实现:
public int threeSumMulti(int[] A, int target) {
int MOD = 1_000_000_007; // 因为最大数为10^9+7
// 计算数组中不同元素出现的个数 因为 0 <= A[i] <=100
long[] c = new long[101]; // 定义成long,在计算中不用转换
for (int a : A) c[a]++;
long ans = 0;
for (int i = 0; i <= target; i++) {
for (int j = i; j <= target; j++) {
int k = target - i - j;
if (k < 0 || k >= c.length || k < j) continue;
if (c[i] == 0 || c[j] == 0 || c[k] == 0) continue;
if (i ==j && j == k) {
ans += (c[i] - 2) * (c[i] - 1) * c[i] / 6;
} else if (i ==j && j != k) {
ans += c[i] * (c[i] - 1) / 2 * c[k];
} else if (i != j && j == k) {
ans += c[i] * (c[j] - 1) * c[j] / 2;
} else {
ans += c[i] * c[j] * c[k];
}
ans %= MOD;
}
}
return (int)ans;
}
Reference
花花酱 LeetCode 923. 3Sum With Multiplicity - 刷题找工作 EP227
923. 3Sum With Multiplicity - LeetCode的更多相关文章
- [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况
Given an integer array A, and an integer target, return the number of tuples i, j, k such that i &l ...
- LeetCode 923. 3Sum With Multiplicity
原题链接在这里:https://leetcode.com/problems/3sum-with-multiplicity/ 题目: Given an integer array A, and an i ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
- 【leetcode】923. 3Sum With Multiplicity
题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k such tha ...
- [Swift]LeetCode923.三数之和的多种可能 | 3Sum With Multiplicity
Given an integer array A, and an integer target, return the number of tuples i, j, k such that i &l ...
- arts-week14
Algorithm 923. 3Sum With Multiplicity - LeetCode Review Building a network attached storage device w ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- 算法与数据结构基础 - 双指针(Two Pointers)
双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
随机推荐
- java模板设计
- 10分钟go crawler colly从入门到精通
Introduction 本文对colly如何使用,整个代码架构设计,以及一些使用实例的收集. Colly是Go语言开发的Crawler Framework,并不是一个完整的产品,Colly提供了类似 ...
- stm32学习总结)—SPI-FLASH 实验 _
SPI总线 SPI 简介 SPI 的全称是"Serial Peripheral Interface",意为串行外围接口,是Motorola 首先在其 MC68HCXX 系列处理器上 ...
- WebView的一些简单用法
一直想写一个关于 WebView 控件的 一些简单运用,都没什么时间,这次也是挤出时间写的,里面的一些基础知识就等有时间再更新讲解一下,今天就先把项目出来做一些简单介绍,过多的内容可以看我的源码,都传 ...
- 微信小程序页面跳转参数传递
可以使用标签直接传递 <navigator class="gotoDetail" target="self" url="../detail/de ...
- 如何使用Android可视化埋点
Android可视化埋点是Android全埋点的增强.开发者可以将App界面同步至DTM界面,并在DTM界面通过可视化点击的方式添加埋点事件.目前Android可视化埋点包含两种埋点方式:普通可视化埋 ...
- angular.js中 路由 用法及概念
在开讲之前,首先谈谈APP应用.平时我们用的app总是多页面,如果用原生安卓或者苹果,那当然很流畅啦.但是当我们用一般的html页面做移动端,简单时候我们可以用<a href="&qu ...
- 2021.12.15 P2328 [SCOI2005]超级格雷码(找规律填空)
2021.12.15 P2328 [SCOI2005]超级格雷码(找规律填空) https://www.luogu.com.cn/problem/P2328 题意: 输出n位B进制的格雷码. 分析: ...
- k8s 新版本 部署 Ingress-nginx controller
k8s 新版本 部署 Ingress-nginx controller 本篇主要记录一下 k8s 新版本 1.23.5 中如何搭建 ingress controller 以及里面的注意项 新版本和老版 ...
- Spring Security实现基于RBAC的权限表达式动态访问控制
昨天有个粉丝加了我,问我如何实现类似shiro的资源权限表达式的访问控制.我以前有一个小框架用的就是shiro,权限控制就用了资源权限表达式,所以这个东西对我不陌生,但是在Spring Securit ...