【leetcode】923. 3Sum With Multiplicity
题目如下:
Given an integer array
A, and an integertarget, return the number of tuplesi, j, ksuch thati < j < kandA[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 <= 30000 <= A[i] <= 1000 <= 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 ...
随机推荐
- IntelliJ IDEA 代码调式
Mute Breakpoints: 保留所有断点,但是不执行(程序一次性执行). Condition: 条件触发 即当执行到i的值变为5的时候,在断点处暂停.
- python自动刷新抢火车票
#!/usr/bin/env python #-*- coding: utf-8 -*- """ 火车票 可以自动填充账号密码,同时,在登录时,也可以修改账号密码 然后手 ...
- cocos2D-X Doc
{ //https://docs.cocos2d-x.org/api-ref/index.html //https://docs.cocos.com/cocos2d-x/manual/zh/sprit ...
- excel VBA 编程
Dim cuttent_columns As Integer Dim care_repeat As Integer Private Sub Workbook_Open() Dim i As Integ ...
- python-字符、字符串、函数处理
1.列表元祖字典集合 列表 list = ["a", "b", "c", "d"] 元祖 tup = (1, 2, 3, ...
- getJSON方式请求服务器
register.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- 阿里云基于OSS的云上统一数据保护方案2.0技术解析
近年来,随着越来越多的企业从传统经济向数字经济转型,云已经渐渐成为数据经济IT新常态.核心业务系统上云,云上的业务创新,这些都产生了大量的业务数据,这些数据也成为了企业最重要的资产.资源. 阿里云基于 ...
- JS中数据结构之队列
队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素.队列用于存储按 顺序排列的数据,先进先出. 队列的两种主要操作是:向队列中插入新元素和删除队列中的元素.插入操作也叫做入 队,删除操作也 ...
- Android中实现Activity的启动拦截之----实现360卫士的安装应用界面
第一.摘要 今天不是周末,但是我已经放假了,所以就开始我们的技术探索之旅,今天我们来讲一下Android中最期待的技术,就是拦截Activity的启动,其实我在去年的时候,就像实现这个技术了,但是因为 ...
- Python基础教程(018)--官方解释器交互运行
前言: 在交互式运行Python程序 内容 在Python的shell中直接输入Python的代码,可以立即执行结果 交互式运行Python的优缺点 1,缺点--代码不能保存 2,不适合运行太大的程序 ...