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. 顺利通过EMC实验(6)

  2. ECMAScript中有两种属性:数据属性和访问器属性。

    ECMA-262定义这些特性是为了实现JavaScript引擎用的,因此在JavaScript中不能直接访问它们.为了表示特性是内部值,该规范把它们放在了两对儿方括号中,例如 [[Enumerable ...

  3. IOS中弹出键盘后出现fixed失效现象的解决方案

    概述 这个问题常出现在移动web开发中聊天或者留言页面的绝对定位输入框上,页面超过屏幕大小时候输入框focus状态下(键盘弹出)绝对定位的元素失效,导致页面滚动时候把定位元素一并带走,体验十分不好,在 ...

  4. 前端网络安全——Cookies

    一.Cookies特性 1.前端数据存储 2.后端通过http头设置 3.请求时通过http头传给后端 4.前端可读写 5.遵守同源策略 二.Cookies内容 1.域名 2.有效期,删除cookie ...

  5. 修改Menu_item的字体属性

    前面一直在找 MenuItem的文字颜色的设置.我发现API中只有背景颜色的设置... 所以找到下面的方法.在OverFlow上看到的.在onCreateOptionsMenu中覆写一下, 使Menu ...

  6. 关于iOS 二维数组,对象映射的问题

    数据格式如下: 遇到的问题是二维数组的 对象无法 通过 yymodel 直接实力话 ~~~ -"scoring_probability_distribution": [ -[ -{ ...

  7. 厉害!我带的实习生仅用四步就整合好SpringSecurity+JWT实现登录认证!

    小二是新来的实习生,作为技术 leader,我还是很负责任的,有什么锅都想甩给他,啊,不,一不小心怎么把心里话全说出来了呢?重来! 小二是新来的实习生,作为技术 leader,我还是很负责任的,有什么 ...

  8. Android 环境搭建记录

    Android 环境搭建记录 官网 https://developer.android.com/ studio 下载地址 官方下载 jikexueyuanwiki 国内镜像 studio历史版本 安装 ...

  9. 帝国CMS 给简介字段添加一键排版按钮

    帝国CMS后台->管理数据表->选择数据表>打开smalltext字段输入表单替换html代码 添加如下代码: <script> function format() { ...

  10. 序列化之Serializer类与ModelSerializer类的使用

    序列化之Serializer类的使用(5星) 作用: 序列化,序列化器会把模型对象转换成字典,经过response以后变成json字符串 反序列化,把客户端发送过来的数据,经过request以后变成字 ...