原题链接在这里:https://leetcode.com/problems/3sum-with-multiplicity/

题目:

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:

  1. 3 <= A.length <= 3000
  2. 0 <= A[i] <= 100
  3. 0 <= target <= 300

题解:

Perform 3Sum and find the target.

Here there are 2 cases:

1. A[l] != A[r], count the frequency of A[l] and A[r], possible number of tuples is leftCount*rightCount.

e.g. 2333, A[l] = 2, A[r] = 3, leftCount = 1, rightCount = 3, number of types is 1*3 = 3.

2. A[l] == A[r], number of tupes is count*(count-1)/2.

e.g. 222, A[l] = 2, A[r] = 2, number of tupes 3*2/2 = 3. There are 3 ways to pick 2 elements from it.

Time Compelxity: O(n^2). n = A.length.

Space: O(1).

AC Java:

 class Solution {
public int threeSumMulti(int[] A, int target) {
if(A == null || A.length == 0){
return 0;
} int res = 0;
int M = 1000000000+7;
Arrays.sort(A);
for(int i = 0; i<A.length-2; i++){
int l = i+1;
int r = A.length-1;
while(l<r){
int sum = A[i] + A[l] + A[r];
if(sum < target){
l++;
}else if(sum > target){
r--;
}else if(A[l] != A[r]){
int leftCount = 1;
int rightCount = 1;
while(l+1<r && A[l]==A[l+1]){
leftCount++;
l++;
} while(l+1<r && A[r]==A[r-1]){
rightCount++;
r--;
} res += leftCount*rightCount;
res = res%M;
l++;
r--;
}else{
// A[l] == A[r]
res += (r-l+1)*(r-l)/2;
res = res%M;
break;
}
}
} return res;
}
}

LeetCode 923. 3Sum With Multiplicity的更多相关文章

  1. [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 ...

  2. 923. 3Sum With Multiplicity - LeetCode

    Question 923. 3Sum With Multiplicity Solution 题目大意: 给一个int数组A和一个目标值target,求满足下面两个条件的组合个数,其中i,j,k分别为数 ...

  3. 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...

  4. 【leetcode】923. 3Sum With Multiplicity

    题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k  such tha ...

  5. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  6. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  7. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  8. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  9. [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 < ...

随机推荐

  1. python_dict json读写文件

    命令汇总: json.dumps(obj) 将python数据转化为json Indent实现缩进,ensure_ascii 是否用ascii解析 json.loads(s) 将json数据转换为py ...

  2. Spark之RDD弹性特性

    RDD作为弹性分布式数据集,它的弹性具体体现在以下七个方面. 1.自动进行内存和磁盘数据存储的切换 Spark会优先把数据放到内存中,如果内存实在放不下,会放到磁盘里面,不但能计算内存放下的数据,也能 ...

  3. MySQL8.0 下载安装启动(Windows10)

    2019年6月13日20:13:21 MySQL8.0 下载安装启动(Windows10) 下载 下载地址:https://dev.mysql.com/downloads/mysql/8.0.html ...

  4. Java单元测试 Http Server Mock框架选型

    背景动机 某期优化需要针对通用的HttpClient封装组件--HttpExecutor在保证上层暴露API不动的前提做较多改动,大致包括以下几点: apache http client 版本升级 H ...

  5. C# 练习题 判断1至输入数值之间有多少个素数,并输出所有素数。

    题目:判断1至输入数值之间有多少个素数,并输出所有素数.1.程序分析:判断素数的方法:用一个数分别去除2到当前数-1,如果能被整除,则表明此数不是素数,反之是素数. class Program { / ...

  6. P3121 [USACO15FEB]审查(AC自动机)

    题目: P3121 [USACO15FEB]审查(黄金)Censoring (Gold) 解析: 多字符串匹配,首先想到AC自动机 建立一个AC自动机 因为有删除和拼接这种操作,考虑用栈维护 顺着文本 ...

  7. vsftp 安装配置(被动模式)

    vi /etc/vsftpd/vsftpd.conf vsftp配置末尾添加 pasv_enable=YES pasv_min_port=10000 pasv_max_port=10030 防火墙端口 ...

  8. JavaScript之轮播图

    (1)html <div class="box" id="box"> <ul class="uls" id="u ...

  9. 基于MUI框架+HTML5PLUS 开发 iOS和Android 应用程序(APP)

    目录 事前准备 创建项目 利用MUI写一个简单的页面 关于文件打包 事前准备 # 软件 HBuilder X Web开发IDE 下载地址:https://www.dcloud.io/hbuilderx ...

  10. GO执行shell命令

    Golang执行shell命令主要依靠exec模块 代码为核心逻辑,并非全部 运行命令 cmd1 = exec.Command("ls") if err = cmd1.Run(); ...