【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/ 题目地址 ...
随机推荐
- R语言与医学统计图形-【20】ggplot2图例
ggplot2绘图系统--图例:guide函数.标度函数.overrides.aes参数 图例调整函数guide_legend也属于标度函数,但不能单独作为对象使用,即不能如p+guide_legen ...
- rabbit mq的php使用 amqp 的支持
rabbit mq的php使用 php想要操作rabbit 需要扩展amqp 1,先查看自己的php版本 phpinfo() 接下来下载dll文件 地址http://pecl.php.net/pack ...
- 计算机网络-4-7-内部网关协议OSPF
内部网关协议OSPF(开放最短路径优先) 出现的原因:为了克服RIP协议的缺点在1989年开发出来,**开放 表明OSPF协议不受任何厂家的限制.最短路径优先是因为使用了最短路径算法SPF**. OS ...
- 1小时学会Git玩转GitHub
版权声明:原创不易,本文禁止抄袭.转载,侵权必究! 本次教程建议一边阅读一边用电脑实操 目录 一.了解Git和Github 1.1 什么是Git 1.2 什么是版本控制系统 1.3 什么是Github ...
- day30线程(Threads)
day30线程(Threads) 1.开启线程 一.什么是线程: 1.进程是资源分配的最小单位,线程是CPU调度的最小单位.每一个进程中至少有一个线程. 2.主进程中的线程称为主线程,其他开启的线程称 ...
- 【编程思想】【设计模式】【行为模式Behavioral】观察者模式Observer
Python转载版 https://github.com/faif/python-patterns/blob/master/behavioral/observer.py #!/usr/bin/env ...
- 什么是maven(二)
转自博主--一杯凉茶 maven项目构建ssh工程(父工程与子模块的拆分与聚合) 前一节我们明白了maven是个什么玩意,这一节就来讲讲他的一个重要的应用场景,也就是通过maven将一个ssh项目 ...
- 【C/C++】小红的字符串 / 中兴捧月
考试的时候想复杂了,其实直接一边写放进set里去重就可以了 很有意思 自己的理解就是cpp的map+set或者就是set可以完成大多数java的hashset操作 链接:https://ac.nowc ...
- 匿名内部类与lamda表达式
1.为什么要使用lamda表达式 从JDK1.8开始为了简化使用者进行代码开发,专门提供有Lambda表达式的支持,利用此操作形式可以实现函数式的编程,对于函数式编程比较著名的语言:haskell,S ...
- 学习 27 门编程语言的长处,提升你的 Python 代码水平
Python猫注:Python 语言诞生 30 年了,如今的发展势头可谓如火如荼,这很大程度上得益于其易学易用的优秀设计,而不可否认的是,Python 从其它语言中偷师了不少.本文作者是一名资深的核心 ...