题目如下:

Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target.

As the answer can be very large, return it modulo 10^9 + 7.

Example 1:

Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation:
Enumerating by the values (A[i], A[j], A[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.

Example 2:

Input: A = [1,1,2,2,2,2], target = 5
Output: 12
Explanation:
A[i] = 1, A[j] = A[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.

Note:

  1. 3 <= A.length <= 3000
  2. 0 <= A[i] <= 100
  3. 0 <= target <= 300

解题思路:虽然A.length 最大值是300,但是A[i]的值在0~100之间,说明A中有很多重复值,对A去重后length最大值也就100,所以O(n^3)的复杂度完全可以接受。首先对A去重,假设A[i] * A[j] * A[k] == target (i<=j<=k),那么 A[i] 、A[j]、A[k] 三者之间的值有这么几种情况:

a.三者相等: 这种情况,一共存在C(A[i]在A中的个数,3)种组合 (A[i]在A中的个数 >= 3, 这个表达的是A[i]在去重前的A出现的次数)

b.任意两者相等:假设A[i] == A[j] != A[k] ,那么一共存在 C(A[i]在A中的个数,2) * A[k]在A中出现的次数 (A[i]在A中的个数,2) >= 2)

c.三者完全不同:这个最简单,一共存在 A[i]在A中出现的次数 * A[j]在A中出现的次数 * A[k]在A中出现的次数

代码如下:

class Solution(object):
def threeSumMulti(self, A, target):
"""
:type A: List[int]
:type target: int
:rtype: int
"""
def combination(n,m):
v1 = 1
times = 0
while times < m:
v1 *= n
n -= 1
times += 1
v2 = 1
while m > 0:
v2 *= m
m -= 1
return v1 / v2 dic = {}
for i in A:
dic[i] = dic.setdefault(i, 0) + 1
ul = list(set(A))
res = 0
for i in range(len(ul)):
for j in range(i,len(ul)):
for k in range(j,len(ul)):
if (ul[i] + ul[j] + ul[k]) != target:
continue
elif ul[i] == ul[j] == ul[k]:
if dic[ul[i]] >= 3:
res += combination(dic[ul[i]],3)
elif ul[i] == ul[j]:
if dic[ul[i]] >= 2:
res += (combination(dic[ul[i]],2) * dic[ul[k]])
elif ul[i] == ul[k]:
if dic[ul[i]] >= 2:
res += (combination(dic[ul[i]], 2) * dic[ul[j]])
elif ul[j] == ul[k]:
if dic[ul[j]] >= 2:
res += (combination(dic[ul[j]], 2) * dic[ul[i]])
else:
res += (dic[ul[i]] * dic[ul[j]] * dic[ul[k]])
return res % (pow(10,9) + 7)

【leetcode】923. 3Sum With Multiplicity的更多相关文章

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

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

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

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

  3. 【LeetCode】16. 3Sum Closest 最接近的三数之和

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

  4. 【LeetCode】259 3Sum Smaller

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  5. 【LeetCode】16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  6. 【LeetCode】15. 3Sum 三个数和为0

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  7. 【leetcode】15. 3Sum

    题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  8. 【LeetCode】015 3Sum

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  9. 【LeetCode】016 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

随机推荐

  1. 英语单词profile

    profile 来源——linux系统文件名 [root@centos71 ~]# cat /etc/profile # /etc/profile # System wide environment ...

  2. 学习总结-Redis

    一,简介 redis(Remote Dictionary Server)是一种Nosql技术,它是一个开源的高级kv存储和数据结构存储系统,它经常被拿来和Memcached相比较,但是Memcache ...

  3. php strip_tags()函数 语法

    php strip_tags()函数 语法 作用:剥去字符串中的 HTML 标签 语法:strip_tags(string,allow) 参数: 参数 描述 string  必须,规定要检查的字符串. ...

  4. 31 July

    P1005 矩阵取数游戏 高精度不能更坑-- #include <cstdio> #include <cstring> struct INT { long long h, l; ...

  5. Linux 删除特殊文件名的文件

    1.文件名含有特殊字符: 1) 执行 ls -i 命令 ,文件前面会出现一个数字,这个数字是文件的节点号 2) 使用find命令删除 find ./ -inum 节点号 -delete 2.文件名是以 ...

  6. 资源-DotNet-站点:DotNet 站点列表

    ylbtech-资源-DotNet-站点:DotNet 站点列表 1.ASP.NET Web返回顶部 1.1.问卷星 https://www.wjx.cn/sample/service.aspx 1. ...

  7. 104、Tensorflow 的变量重用

    import tensorflow as tf # 在不同的变量域中调用conv_relu,并且声明我们想创建新的变量 def my_image_filter(input_images): with ...

  8. ssd存储的SLC、MLC、TLC闪存芯片颗粒有什么区别?

    SLC = Single-Level Cell ,即1bit/cell,速度快寿命长,价格贵(约MLC 3倍以上的价格),约10万次擦写寿命: MLC = Multi-Level Cell,即2bit ...

  9. 【awk】 处理多个文件

    处理多个文件: 1. 可以在代码中指定读取某个文件, 其他的用命令行输入           while ( geline < "file.txt" > 0 ) {   ...

  10. PAT甲级【2019年3月考题】——A1157 Anniversary【25】

    Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...