【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 ...
随机推荐
- 【NLP新闻-2013.06.16】Representative Reviewing
英语原文地址:http://nlp.hivefire.com/articles/share/40221/ 注:本人翻译NLP新闻只为学习专业英语和扩展视野,如果翻译的不好,请谅解! (实在是读不大懂, ...
- 【leetcode】543. Diameter of Binary Tree
题目如下: 解题思路:最长的周长一定是树中某一个节点(不一定是根节点)的左右子树中的两个叶子节点之间的距离,所以最简单的办法就是把树中所有节点的左右子树中最大的两个叶子节点之间的距离求出来,最终得到最 ...
- Python基础教程(022)--Pycharm快速体验
前言 熟悉掌握Python工具 内容 提示 断点调试 目的 学会了解Pycharm的使用 掌握Pycharm执行程序 掌握断点调试模式
- 【HDOJ6611】K Subsequence(费用流)
题意:给定一个长为n的正整数序列,要求从中取出至多k个不下降序列,使得它们的和最大,求这个和 n<=2e3,k<=10,a[i]<=1e5 思路:极其考验模板,反正我的spfa和zk ...
- 【2019 Multi-University Training Contest 3】
01: 02:https://www.cnblogs.com/myx12345/p/11593829.html 03: 04:https://www.cnblogs.com/myx12345/p/11 ...
- nginx配置-location
以 =开头表示精确匹配如 A 中只匹配根目录结尾的请求,后面不能带任何字符串. ^~ 开头表示uri以某个常规字符串开头,不是正则匹配 ~ 开头表示区分大小写的正则匹配; ~* 开头表示不区分大小写的 ...
- (63)C# 不安全代码unsafe
unsafe fixed stackalloc void*
- python排序算法-冒泡和快速排序,解答阿里面试题
''常见的排序算法\ 插入排序/希尔排序/直接排序/堆排序 冒泡排序/快速排序/归序排序/基数排序 给定一个列表,将这个列表进行排序,要求:> 时间复杂度要小于O(n^2) 复杂度:1.时间复杂 ...
- shell查词典
curl http://cn.bing.com/dict/search?q=spawn -s | sed -e '{s/<\/span>/&\n/g}' | sed -n '{/& ...
- T1215:迷宫
[题目描述] 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行.同时当Extense处在某 ...