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 <= 30000 <= A[i] <= 1000 <= 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 < ...
随机推荐
- celery 分布式异步任务框架(celery简单使用、celery多任务结构、celery定时任务、celery计划任务、celery在Django项目中使用Python脚本调用Django环境)
一.celery简介: Celery 是一个强大的 分布式任务队列 的 异步处理框架,它可以让任务的执行完全脱离主程序,甚至可以被分配到其他主机上运行.我们通常使用它来实现异步任务(async tas ...
- 阿里云主机centos7系统创建SWAP区,并启动挂载(适合无SWAP区虚拟化平台)
以root用户登录建立交换区文件: fallocate -l 2G /swapfile /swapfile //赋予仅root用户的权限,确保安全 mkswap /swapfile swapon /s ...
- HBase 系列(九)——HBase 容灾与备份
一.前言 本文主要介绍 Hbase 常用的三种简单的容灾备份方案,即CopyTable.Export/Import.Snapshot.分别介绍如下: 二.CopyTable 2.1 简介 CopyTa ...
- Python进阶(七)----带参数的装饰器,多个装饰器修饰同一个函数和递归简单案例(斐波那契数列)
Python进阶(七)----带参数的装饰器,多个装饰器修饰同一个函数和递归简单案例(斐波那契数列) 一丶带参数的装饰器 def wrapper_out(pt): def wrapper(func): ...
- pandas-11 TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely错误解决方法
pandas-11 TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be sa ...
- 用vue-cli搭建vue项目
首先需要明确的是:Vue.js 不支持 IE8 及其以下 IE 版本,一般用与移动端,基础:开启最高权限的DOS命令(否则会出现意外的错误提示) 一.安装node.js,检测版本node -v,还要检 ...
- 使用原生JS 修改 DIV 属性
本例参考并改进自:https://www.jianshu.com/p/2961d9c317a3 大家可以一起学习!! <!DOCTYPE html> <html lang=" ...
- 仿EXCEL插件,智表ZCELL产品V1.7 版本发布,增加自定义右键菜单功能
详细请移步 智表(ZCELL)官网www.zcell.net 更新说明 这次更新主要应用户要求,主要解决了自定义右键菜单事件的支持,并新增了公式中自定义函数传参.快捷键剪切等功能,欢迎大家体验使用. ...
- npm 安装、卸载 模块或包的命令(转载)
npm安装卸载命令 来源:https://www.jianshu.com/p/e6ee00ea03cd npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目 ...
- index.jsp乱码问题的解决
我们在做java项目的时候,都会有个首页,一般就是index.jsp,然后在index.jsp中引入相关的文件,一般也是引入打包过后的相关资源文件. 当index.jsp上面的中文出现乱码的时候,就需 ...