【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/ 题目地址 ...
随机推荐
- confluence——实现
基于CentOS6.9 [root@localhost ~]# yum install mysql mysql-server -y [root@localhost ~]#yum install -y ...
- accurate, accuse
accurate accurate(不是acute)和precise是近义词,precise里有个pre,又和excise(切除, 不是exercise),concise一样有cise.Why? 准确 ...
- 零基础学习java------day9------多态,抽象类,接口
1. 多态 1.1 概述: 某一个事务,在不同环境下表现出来的不同状态 如:中国人可以是人的类型,中国人 p = new 中国人():同时中国人也是人类的一份,也可以把中国人称为人类,人类 d ...
- STM32一些特殊引脚做IO使用的注意事项
1 PC13.PC14.PC15的使用 这三个引脚与RTC复用,<STM32参考手册>中这样描述: PC13 PC14 PC15需要将VBAT与VDD连接,实测采用以下程序驱动4个74HC ...
- Linux下删除的文件如何恢复
Linux下删除的文件如何恢复 参考自: [1]linux下误操作删除文件如何恢复 [2]Linux实现删除撤回的方法 以/home/test.txt为例 1.df -T 文件夹 找到当前文件所在磁盘 ...
- oracle(数据备份)
1 --oracle数据备份(三种方法) 2 --1.逻辑备份与恢复:用Oracle提供的工具,导入/导出(exp,imp),数据 3 --泵导入/导出(impdp,expdp),装入器(SQL*Lo ...
- react-native环境搭建完后,用genymotion运行出错的处理方法
以下方法是争对react-native 0.63版本的 出错提示如下: 模拟器点击reload后,如下提示: 找了网上很多方法,很多都是旧版本的bug处理的方法,没有用,后面经过摸索发现,原来原因是 ...
- SpringBoot项目找不到主类或无法加载主类
问题描述 启动springboot项目的时候发现启动失败,查看日志发现因为找不到主类或无法加载主类. 解决 我这个项目是拉取的别人git上的项目,看了一下目录结构发现没有编译后的文件(target目录 ...
- Maven配置大全
maven项目打jar包(带依赖) <build> <plugins> <plugin> <artifactId>maven-assembly-plug ...
- 【JavaWeb】【Maven】001 下载与配置
Maven下载与配置 Download Url:Maven – Download Apache Maven After downloading it, unpack it and configure ...