Question

923. 3Sum With Multiplicity

Solution

题目大意:

给一个int数组A和一个目标值target,求满足下面两个条件的组合个数,其中i,j,k分别为数组的索引

  1. i<j<k
  2. 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的更多相关文章

  1. [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 ...

  2. LeetCode 923. 3Sum With Multiplicity

    原题链接在这里:https://leetcode.com/problems/3sum-with-multiplicity/ 题目: Given an integer array A, and an i ...

  3. 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...

  4. 【leetcode】923. 3Sum With Multiplicity

    题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k  such tha ...

  5. [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 ...

  6. arts-week14

    Algorithm 923. 3Sum With Multiplicity - LeetCode Review Building a network attached storage device w ...

  7. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  8. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  9. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

随机推荐

  1. java模板设计

  2. 搞半天,全国34个省份包含湾湾\香港\澳门的高德poi兴趣点23类数据终于爬完事了

    1.技术架构: python+阿里云数据库mongodb5.0+高德地图rest api 2.成本: 阿里云数据库mongodb5.0一个月话费1k多 2.遇到的问题 1)两个阿里云账号下 mongo ...

  3. RStudio中文乱码

    解决办法一: 1.设置RStudio文本显示的默认编码:RStudio菜单栏的Tools -> Global Options 2.code-->saving-->default te ...

  4. java中抽象类和抽象方法到底有什么用呢?

    抽象类和抽象方法有什么用呢?马克-to-win:当初sun公司为什么要设计抽象类和抽象方法呢?当你在做车的系统设计时,当你设计车这个通用类时,假如你确认别人实例化车这个通用类没有意义时(不知道是bik ...

  5. EMS设置发送连接器和接收连接器邮件大小

    任务:通过EMS命令设置发送接收连接器和接收连接器的邮件大小限制值为50MB. 以Exchange管理员身份打开EMS控制台.在PowerShell命令提示符下. 键入以下命令设置接收-连接器的最大邮 ...

  6. 居中select中的option选项

    select经常有长短不一的选项:选择不同的选项居中不会生效: 使用text-align:center;text-align-last: center;  可以让所有选项都居中: 关于text-dec ...

  7. FreeSql的各种工程demo上新啦

    FreeSql的各种工程demo GitHub | Gitee console,winforms nf461,vb,wpf,webapi,workerSevice,signalIR xamarinFo ...

  8. JavaWeb学习第一阶段结束

    模仿狂神实现简单的用户增删改查,增加了前端登录时的密码验证 JavaWeb学习第一阶段结束,相较于第一阶段的一味学习,第二阶段想拿出更多的时间来阅读别人的源码以及跟着做简单的小项目,同时进一步深入学习 ...

  9. css 实现图片专场

    <template> <div> </div> </template> <style lang="scss"> html ...

  10. 轻量迅捷时代,Vite 与Webpack 谁赢谁输

    你知道Vite和Webpack吗?也许有不少"程序猿"对它们十分熟悉. Webpack Webpack是一个JavaScript应用程序的静态模块打包工具,它会对整个应用程序进行依 ...