【leetcode】923. 3Sum With Multiplicity
题目如下:
Given an integer array
A
, and an integertarget
, return the number of tuplesi, j, k
such thati < j < k
andA[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:
3 <= A.length <= 3000
0 <= A[i] <= 100
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的更多相关文章
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- noteone
- 纯JSP简单登录实例
记一下,免得以后忘记了,又要去查. 文件共有四个web.xml.login.jsp.logout.jsp.welcome.jsp四个文件 测试环境:Tomcat 6.0.x 假设项目名称是LoginS ...
- boost location-dependent times
1. local_date_time #include <boost/date_time/local_time/local_time.hpp> #include <iostream& ...
- 偏函数(partial)
from functools import partial def add(a,b,c,d): return a+b+c+d add = partial(add,1,2) print(add(3,4) ...
- FMXUI ANDROID下连续按多次返回出现异常
在ANDROID下,按返回键后,默认是关闭当前Frame,但连接按返回键,会对当前Frame执行多次关闭动作,因为已经释放过对象,再次关闭会出现异常错误,解决办法:定义一个标识如FClosed: ...
- 一文带你领略虚拟化领域顶级技术会议KVM Forum 2018
KVM Forum是由Linux基金会组织的高端技术论坛会议,主要为社区各个维护者,开发人员,和用户提供一个讨论Linux虚拟化技术发展趋势以及挑战的交流场所.参会人员都集中在KVM虚拟化相关领域,是 ...
- 常见Serialize技术探秘(ObjectXXStream、XML、JSON、JDBC byte编码、Protobuf)
目前业界有各种各样的网络输出传输时的序列化和反序列化方案,它们在技术上的实现的初衷和背景有较大的区别,因此在设计的架构也会有很大的区别,最终在落地后的:解析速度.对系统的影响.传输数据的大小.可维护性 ...
- AcWing 252. 树 (点分治)打卡
题目:https://www.acwing.com/problem/content/254/ 题意:求一棵树上,路径<=k的有多少条 思路:点分治,我们用两个指针算solve函数,首先对算出来的 ...
- 关于vsftp出现Restarting vsftpd (via systemctl): Job for vsftpd.service failed because the control 的解决办法
转载于:http://blog.csdn.net/it_dream_er/article/details/50783111 刚刚在搭建ftp服务器时,在配置好一切的参数之后,在我重启时,出现了无法启动 ...
- pssh系列命令详解
安装 pssh提供OpenSSH和相关工具的并行版本.包括pssh,pscp,prsync,pnuke和pslurp.该项目包括psshlib,可以在自定义应用程序中使用.pssh是python写的可 ...