【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/3sum-with-multiplicity/description/
题目描述:
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:
- 3 <= A.length <= 3000
- 0 <= A[i] <= 100
- 0 <= target <= 300
题目大意
在一个数组中找出有多少组合的和是target。
解题方法
看到3sum直接就3sum走起啊!因为需要统计总共出现的次数,那么直接暴力肯定是不行的,需要我们先统计每个数字出现了多少次,过会进行一个查找和组合。使用了set和list来保存去重的数字。
两重循环i, j,分别对应了第一二个数字,需要注意的是第二个数字和第一个数字相同,所以j从i开始向后遍历。第三个数字等于target减去前两个数字,比较重要的一步是需要判断第三个数字要不比第二个数字小,而且第三个数字必须在set中,因为第三个数字不能向前找,得向后找,而且可以等于第二个数字!
如果把上面的a, b, c全都找到了,那么底下的方法就很简单了,求一个组合数字!从counter里面知道每个数字出现了多少次,判断一下,这三个数字是不是都不相等、有两个相等、三个全相等,这三种情况,然后就知道了总的数字组合会出现多少次。
最后最后,需要模一个数,就是因为忘了求模,导致又浪费了一次提交。。
时间复杂度是O(N^2),空间复杂度是O(N)。
class Solution(object):
def threeSumMulti(self, A, target):
"""
:type A: List[int]
:type target: int
:rtype: int
"""
count = collections.Counter(A)
Aset = set(A)
Alist = list(Aset)
Alist.sort()
_lenA = len(Alist)
res = 0
for i in range(_lenA):
for j in range(i, _lenA):
c = target - Alist[i] - Alist[j]
if c >= Alist[j] and c in Aset:
if Alist[i] != Alist[j] != c:
res += count[Alist[i]] * count[Alist[j]] * count[c]
elif Alist[i] == Alist[j] and Alist[j] != c:
res += count[c] * self.caculate(count[Alist[i]], 2)
elif Alist[j] == c and Alist[i] != Alist[j]:
res += count[Alist[i]] * self.caculate(count[Alist[j]], 2)
elif Alist[i] == c and Alist[j] != c:
res += count[Alist[j]] * self.caculate(count[c], 2)
else:
res += self.caculate(count[Alist[i]], 3)
return res % (10 ** 9 + 7)
def caculate(self, x, i):
if i == 2:
return x * (x - 1) / 2
elif i == 3:
return x * (x - 1) * (x - 2) / 6
参考资料:
日期
2018 年 10 月 14 日 —— 周赛做出来3个题,开心
【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)的更多相关文章
- [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】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 923. 3Sum With Multiplicity - LeetCode
Question 923. 3Sum With Multiplicity Solution 题目大意: 给一个int数组A和一个目标值target,求满足下面两个条件的组合个数,其中i,j,k分别为数 ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- python12类
self 表示类里面的对象的引用 python一般不需要去解决内存管理,解释器会进行自动给回收 #类鱼类之间空格两行,前面的函数里面也是两行,类里面的方法个一行 class Cat(object): ...
- python18协程
协程是我们自己调度的 进程是系统调度的协程切换很少开销 python3.5之前的实现方法 def yield_test(): """实现协程函数""& ...
- perl FileHandle 模块使用
打开多个文件时必备啊 1 use FileHandle; 2 3 my (%index,%fh); 4 # 创建句柄,这里利用gzip 压缩下 5 foreach my $k(keys %index) ...
- Linux-root管理员创建新用户
Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...
- 准确率,召回率,F值,ROC,AUC
度量表 1.准确率 (presion) p=TPTP+FP 理解为你预测对的正例数占你预测正例总量的比率,假设实际有90个正例,10个负例,你预测80(75+,5-)个正例,20(15+,5-)个负例 ...
- CAN总线常见的两种编码格式(Intel/Motorola)
在汽车电子行业的开发或者测试中,我们经常会看到CAN总线信号的常见的两种编码格式:Intel格式与Motorola格式. 讲解这两种格式之前,我们先来了解一些大端模式和小端模式,会对后面理解这两种编码 ...
- HBase【操作Java api】
一.导入依赖 创建模块,导入以下依赖,maven默认编译版本是1.5,用1.8编译. pom.xml <dependencies> <dependency> <group ...
- css通配样式初始化(多款,供君自选)
腾讯官网 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0; ...
- maven内存溢出解决方法
maven内存溢出(InvocationTargetException: PermGen space) 解决方案:maven脚本:mvn.bat文件@REM set MAVEN_OPTS=-Xdebu ...
- mysql 报 'Host ‘XXXXXX’ is blocked because of many connection errors'
1. 问题:服务启动时,日志报错,导致启动失败: Caused by: com.mysql.cj.exceptions.CJException: null, message from server: ...