LeetCode 923. 3Sum With Multiplicity
原题链接在这里: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:
3 <= A.length <= 3000
0 <= A[i] <= 100
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的更多相关文章
- [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 ...
- 923. 3Sum With Multiplicity - LeetCode
Question 923. 3Sum With Multiplicity Solution 题目大意: 给一个int数组A和一个目标值target,求满足下面两个条件的组合个数,其中i,j,k分别为数 ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
- 【leetcode】923. 3Sum With Multiplicity
题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k such tha ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [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 < ...
随机推荐
- Python学习之路:关于列表(List)复制的那点事
要谈列表的复制,我们就要谈到Python的赋值规则 首先我们创建列表a: a = [1,2,3] 通常我们复制一个元素的方法是这样的: b = a #复制元素的一般方法 print(a) print( ...
- windows中端口查看&关闭进程
在一些情况下遇到的端口占用问题解决: 1.查看端口占用情况 命令:netstat -ano 命令:netstat -ano | findstr 需要释放的端口号 2.查看某端口的占用进程 命令:tas ...
- 删除elasticsearch 30天前的所有索引
我的索引格式为 xxx-xxx-xxx-2019.06.27 xxx-xxxx-2019.06.27 脚本思路: 获取目前 es上所有索引,以日期进行拆分,然后用索引时间对比一个月前的日期,日期小于一 ...
- Aspx后台遍历控件
aspx设计页面 //这个是检测按钮,检测下面的checkbox是否被选中.选中时打印其值 //https://www.cnblogs.com/pwblog/articles/3456385.html ...
- NET Core 3.0 AutoFac替换内置DI的新姿势
原文:NET Core 3.0 AutoFac替换内置DI的新姿势 .NET Core 3.0 和 以往版本不同,替换AutoFac服务的方式有了一定的变化,在尝试着升级项目的时候出现了一些问题. 原 ...
- java之spring之依赖注入
一.DI: Dependency injection; 依赖注入 依赖注入和控制反转是同一个概念的不同说法. 对象的创建依赖于容器.对象属性的设置是由容器来设置. 对象属性的赋值过程称为注入. 二.S ...
- 重新学习Spring2——IOC和AOP原理彻底搞懂
一.AOP 1 Spring AOP 的实现原理 是对OOP编程方式的一种补充.翻译过来为"面向切面编程". 1 AspectJ是静态代理的增强:所谓静态代理就是AOP框架会在便一 ...
- Linux系统快速入门方法
相信看到这篇文章的你一定是想要学习Linux,或者已经在学习Linux的人了,那我们就可以一起探讨一下,学习Linux如何快速入门呢? 首先,希望大家弄清楚自己为什么要学习Linux,有的人是因为兴趣 ...
- Lipo移除ORC架构
Lipo移除ORC架构 打包前检查链接 https://cloud.baidu.com/doc/OCR/OCR-iOS-SDK.html#FAQ cd /Users/guojun/JG-iOS/Pro ...
- 【转载】C#使用ToList()将数组快速转换为List集合
在C#的编程中,数组和List集合是比较常用的两个集合类,有时候因为业务需要,需要将数组集合转换为List集合,此时就可以使用C#中的Linq的扩展方法ToList方法来实现,只需要简单的一条语句即可 ...