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.
class Solution:
def threeSumMulti(self, A: List[int], target: int) -> int:
c = collections.Counter(A)
MOD = 10**9 + 7
res = 0
ll = list(c.keys())
#三个数必须顺序i<j<k 要不有重复
for i in c.keys():
for j in c.keys():
if j > i:
continue
k = target - i - j
if i == j == k: res += c[i] * (c[i] - 1) * (c[i] - 2) // 6
elif i == j != k: res += c[i] * (c[i] - 1) // 2 * c[k] #不太懂为什么这么不是j<k
elif k > i and k > j: res += c[i] * c[j] * c[k]
return res % MOD